1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/textctrl.mm
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/textctrl.h"
22 #include "wx/button.h"
24 #include "wx/settings.h"
25 #include "wx/msgdlg.h"
26 #include "wx/toplevel.h"
30 #include <sys/types.h>
36 #if wxUSE_STD_IOSTREAM
44 #include "wx/filefn.h"
45 #include "wx/sysopt.h"
46 #include "wx/thread.h"
48 #include "wx/osx/private.h"
49 #include "wx/osx/iphone/private/textimpl.h"
51 // currently for some reasong the UITextField leads to a recursion when the keyboard should be shown, so let's leave the code
52 // in case this gets resolved...
54 #define wxOSX_IPHONE_USE_TEXTFIELD 1
59 wxMacEditHelper( UITextView* textView )
61 m_textView = textView;
65 m_formerState = [textView isEditable];
66 [textView setEditable:YES];
73 [m_textView setEditable:m_formerState];
78 UITextView* m_textView;
81 #if wxOSX_IPHONE_USE_TEXTFIELD
83 @interface wxUITextFieldDelegate : NSObject<UITextFieldDelegate>
90 @interface wxUITextField : UITextField
96 @interface wxNSSecureTextField : UITextField<UITextFieldDelegate>
102 @implementation wxNSSecureTextField
106 static BOOL initialized = NO;
110 wxOSXIPhoneClassAddWXMethods( self );
114 - (void)controlTextDidChange:(NSNotification *)aNotification
116 wxUnusedVar(aNotification);
117 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
119 impl->controlTextDidChange();
122 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
124 wxUnusedVar(aNotification);
125 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
128 impl->DoNotifyFocusEvent( false, NULL );
135 @implementation wxUITextFieldEditor
137 - (void) keyDown:(NSEvent*) event
139 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
140 lastKeyDownEvent = event;
141 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
142 [super keyDown:event];
143 lastKeyDownEvent = nil;
146 - (void) keyUp:(NSEvent*) event
148 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
149 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
153 - (void) flagsChanged:(NSEvent*) event
155 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
156 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
157 [super flagsChanged:event];
160 - (BOOL) performKeyEquivalent:(NSEvent*) event
162 BOOL retval = [super performKeyEquivalent:event];
166 - (void) insertText:(id) str
168 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
169 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
171 [super insertText:str];
180 @implementation wxUITextFieldDelegate
182 - (BOOL)textFieldShouldReturn:(UITextField *)textField
184 // the user pressed the "Done" button, so dismiss the keyboard
185 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textField );
188 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
189 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
191 wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId());
192 event.SetEventObject( wxpeer );
193 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
194 wxpeer->HandleWindowEvent( event );
198 [textField resignFirstResponder];
203 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
204 - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
205 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
206 - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
208 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
210 - (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
216 @implementation wxUITextField
220 static BOOL initialized = NO;
224 wxOSXIPhoneClassAddWXMethods( self );
229 - (void)controlTextDidChange:(NSNotification *)aNotification
231 wxUnusedVar(aNotification);
232 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
234 impl->controlTextDidChange();
238 - (BOOL)textFieldShouldReturn:(UITextField *)textField
240 wxUnusedVar(textField);
246 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
248 wxUnusedVar(aNotification);
249 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
252 impl->DoNotifyFocusEvent( false, NULL );
259 @interface wxUITextViewDelegate : NSObject<UITextViewDelegate>
263 - (void)textViewDidChange:(UITextView *)textView;
264 - (void)textViewDidBeginEditing:(UITextView *)textView;
265 - (void)textViewDidEndEditing:(UITextView *)textView;
266 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
270 @implementation wxUITextViewDelegate
272 - (void)textViewDidChange:(UITextView *)textView
274 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
276 impl->controlTextDidChange();
279 - (void)textViewDidBeginEditing:(UITextView *)textView
281 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
283 impl->DoNotifyFocusEvent(true, NULL);
286 - (void)textViewDidEndEditing:(UITextView *)textView
288 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
290 impl->DoNotifyFocusEvent(false, NULL);
293 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
295 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
298 if ( !impl->GetWXPeer()->HasFlag(wxTE_MULTILINE) && [text isEqualToString:@"\n"])
300 [textView resignFirstResponder];
311 // wxUITextViewControl
314 wxUITextViewControl::wxUITextViewControl( wxTextCtrl *wxPeer, UITextView* v) :
315 wxWidgetIPhoneImpl(wxPeer, v),
316 wxTextWidgetImpl(wxPeer)
319 m_delegate= [[wxUITextViewDelegate alloc] init];
321 [m_textView setDelegate:m_delegate];
324 wxUITextViewControl::~wxUITextViewControl()
328 [m_textView setDelegate: nil];
330 [m_delegate release];
333 bool wxUITextViewControl::CanFocus() const
338 wxString wxUITextViewControl::GetStringValue() const
342 wxString result = wxCFStringRef::AsString([m_textView text], m_wxPeer->GetFont().GetEncoding());
343 wxMacConvertNewlines13To10( &result ) ;
346 return wxEmptyString;
349 void wxUITextViewControl::SetStringValue( const wxString &str)
352 wxMacConvertNewlines10To13( &st );
353 wxMacEditHelper helper(m_textView);
356 [m_textView setText: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
359 void wxUITextViewControl::Copy()
362 [m_textView copy:nil];
366 void wxUITextViewControl::Cut()
369 [m_textView cut:nil];
372 void wxUITextViewControl::Paste()
375 [m_textView paste:nil];
378 bool wxUITextViewControl::CanPaste() const
383 void wxUITextViewControl::SetEditable(bool editable)
386 [m_textView setEditable: editable];
389 void wxUITextViewControl::GetSelection( long* from, long* to) const
393 NSRange range = [m_textView selectedRange];
394 *from = range.location;
395 *to = range.location + range.length;
399 void wxUITextViewControl::SetSelection( long from , long to )
401 long textLength = [[m_textView text] length];
402 if ((from == -1) && (to == -1))
409 from = wxMin(textLength,wxMax(from,0)) ;
413 to = wxMax(0,wxMin(textLength,to)) ;
416 NSRange selrange = NSMakeRange(from, to-from);
417 [m_textView setSelectedRange:selrange];
418 [m_textView scrollRangeToVisible:selrange];
421 void wxUITextViewControl::WriteText(const wxString& str)
424 wxMacConvertNewlines10To13( &st );
425 wxMacEditHelper helper(m_textView);
427 wxCFStringRef insert( st , m_wxPeer->GetFont().GetEncoding() );
428 NSMutableString* subst = [NSMutableString stringWithString:[m_textView text]];
429 [subst replaceCharactersInRange:[m_textView selectedRange] withString:insert.AsNSString()];
431 [m_textView setText:subst];
434 void wxUITextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
436 if ([m_textView respondsToSelector:@selector(setFont:)])
437 [m_textView setFont: font.OSXGetUIFont()];
440 bool wxUITextViewControl::GetStyle(long position, wxTextAttr& style)
442 if (m_textView && position >=0)
444 // UIFont* font = NULL;
445 // NSColor* bgcolor = NULL;
446 // NSColor* fgcolor = NULL;
447 // NOTE: It appears that other platforms accept GetStyle with the position == length
448 // but that UITextStorage does not accept length as a valid position.
449 // Therefore we return the default control style in that case.
451 if (position < [[m_textView string] length])
453 UITextStorage* storage = [m_textView textStorage];
454 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
455 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
456 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
460 NSDictionary* attrs = [m_textView typingAttributes];
461 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
462 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
463 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
468 style.SetFont(wxFont(font));
471 style.SetBackgroundColour(wxColour(bgcolor));
474 style.SetTextColour(wxColour(fgcolor));
482 void wxUITextViewControl::SetStyle(long start,
484 const wxTextAttr& style)
487 NSRange range = NSMakeRange(start, end-start);
488 if (start == -1 && end == -1)
489 range = [m_textView selectedRange];
491 UITextStorage* storage = [m_textView textStorage];
493 wxFont font = style.GetFont();
494 if (style.HasFont() && font.IsOk())
495 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
497 wxColour bgcolor = style.GetBackgroundColour();
498 if (style.HasBackgroundColour() && bgcolor.IsOk())
499 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
501 wxColour fgcolor = style.GetTextColour();
502 if (style.HasTextColour() && fgcolor.IsOk())
503 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
508 void wxUITextViewControl::CheckSpelling(bool check)
512 wxSize wxUITextViewControl::GetBestSize() const
519 if (m_textView && [m_textView layoutManager])
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;
530 wxSize sz = r.GetSize();
537 #if wxOSX_IPHONE_USE_TEXTFIELD
540 // wxUITextFieldControl
543 wxUITextFieldControl::wxUITextFieldControl( wxTextCtrl *wxPeer, UITextField* w ) :
544 wxWidgetIPhoneImpl(wxPeer, w),
545 wxTextWidgetImpl(wxPeer)
548 m_delegate = [[wxUITextFieldDelegate alloc] init];
549 [m_textField setDelegate: m_delegate];
550 m_selStart = m_selEnd = 0;
553 wxUITextFieldControl::~wxUITextFieldControl()
556 [m_textField setDelegate: nil];
557 [m_delegate release];
560 wxString wxUITextFieldControl::GetStringValue() const
562 return wxCFStringRef::AsString([m_textField text], m_wxPeer->GetFont().GetEncoding());
565 void wxUITextFieldControl::SetStringValue( const wxString &str)
567 // wxMacEditHelper helper(m_textField);
568 [m_textField setText: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
571 wxSize wxUITextFieldControl::GetBestSize() const
576 wxSize sz = r.GetSize();
582 void wxUITextFieldControl::Copy()
584 [m_textField copy:nil];
587 void wxUITextFieldControl::Cut()
589 [m_textField cut:nil];
592 void wxUITextFieldControl::Paste()
594 [m_textField paste:nil];
597 bool wxUITextFieldControl::CanPaste() const
602 void wxUITextFieldControl::SetEditable(bool editable)
606 void wxUITextFieldControl::GetSelection( long* from, long* to) const
612 void wxUITextFieldControl::SetSelection( long from , long to )
614 long textLength = [[m_textField text] length];
615 if ((from == -1) && (to == -1))
622 from = wxMin(textLength,wxMax(from,0)) ;
626 to = wxMax(0,wxMin(textLength,to)) ;
633 void wxUITextFieldControl::WriteText(const wxString& str)
636 NSEvent* formerEvent = m_lastKeyDownEvent;
637 UIText* editor = [m_textField currentEditor];
640 wxMacEditHelper helper(m_textField);
641 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
646 wxString val = GetStringValue() ;
648 GetSelection( &start , &end ) ;
649 val.Remove( start , end - start ) ;
650 val.insert( start , str ) ;
651 SetStringValue( val ) ;
652 SetSelection( start + str.length() , start + str.length() ) ;
655 m_lastKeyDownEvent = formerEvent;
659 void wxUITextFieldControl::controlAction(WXWidget WXUNUSED(slf),
660 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
662 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
663 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
665 wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId());
666 event.SetEventObject( wxpeer );
667 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
668 wxpeer->HandleWindowEvent( event );
672 bool wxUITextFieldControl::SetHint(const wxString& hint)
674 wxCFStringRef hintstring(hint);
675 [m_textField setPlaceholder:hintstring.AsNSString()];
685 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
686 wxWindowMac* WXUNUSED(parent),
687 wxWindowID WXUNUSED(id),
692 long WXUNUSED(extraStyle))
694 CGRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
695 wxWidgetIPhoneImpl* c = NULL;
696 wxTextWidgetImpl* t = NULL;
697 id<UITextInputTraits> tv = nil;
699 #if wxOSX_IPHONE_USE_TEXTFIELD
700 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
703 UITextView * v = nil;
704 v = [[UITextView alloc] initWithFrame:r];
707 wxUITextViewControl* tc = new wxUITextViewControl( wxpeer, v );
711 #if wxOSX_IPHONE_USE_TEXTFIELD
714 wxUITextField* v = [[wxUITextField alloc] initWithFrame:r];
717 v.textColor = [UIColor blackColor];
718 v.font = [UIFont systemFontOfSize:17.0];
719 v.backgroundColor = [UIColor whiteColor];
721 v.clearButtonMode = UITextFieldViewModeNever;
723 [v setBorderStyle:UITextBorderStyleBezel];
724 if ( style & wxNO_BORDER )
725 v.borderStyle = UITextBorderStyleNone;
727 wxUITextFieldControl* tc = new wxUITextFieldControl( wxpeer, v );
733 if ( style & wxTE_PASSWORD )
734 [tv setSecureTextEntry:YES];
736 if ( !(style & wxTE_MULTILINE) )
738 [tv setAutocorrectionType:UITextAutocorrectionTypeNo];
739 [tv setReturnKeyType:UIReturnKeyDone];
741 [tv setKeyboardType:UIKeyboardTypeDefault];
743 t->SetStringValue(str);
749 #endif // wxUSE_TEXTCTRL