From 99eb484a70f3725340ee1c487f54a0d45f786039 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Wed, 26 Jan 2011 15:51:39 +0000 Subject: [PATCH] support for hints, fixing textfield implementation on iOS git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66770 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/cocoa/private/textimpl.h | 1 + include/wx/osx/core/private.h | 1 + include/wx/osx/iphone/private/textimpl.h | 8 +- include/wx/osx/textctrl.h | 5 + src/osx/cocoa/textctrl.mm | 7 ++ src/osx/iphone/textctrl.mm | 126 +++++++++++++++++------ 6 files changed, 115 insertions(+), 33 deletions(-) diff --git a/include/wx/osx/cocoa/private/textimpl.h b/include/wx/osx/cocoa/private/textimpl.h index 7a800e33b5..4404df8b3d 100644 --- a/include/wx/osx/cocoa/private/textimpl.h +++ b/include/wx/osx/cocoa/private/textimpl.h @@ -39,6 +39,7 @@ public : virtual void SetSelection( long from , long to ); virtual void WriteText(const wxString& str) ; virtual bool HasOwnContextMenu() const { return true; } + virtual bool SetHint(const wxString& hint); virtual void controlAction(WXWidget slf, void* _cmd, void *sender); diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index 5eec53c0c6..741306805a 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -648,6 +648,7 @@ public : virtual wxSize GetBestSize() const { return wxDefaultSize; } + virtual bool SetHint(const wxString& WXUNUSED(hint)) { return false; } private: wxTextEntry * const m_entry; diff --git a/include/wx/osx/iphone/private/textimpl.h b/include/wx/osx/iphone/private/textimpl.h index 5c1bf8d6f1..aab9b49bd8 100644 --- a/include/wx/osx/iphone/private/textimpl.h +++ b/include/wx/osx/iphone/private/textimpl.h @@ -20,7 +20,7 @@ class wxUITextFieldControl : public wxWidgetIPhoneImpl, public wxTextWidgetImpl { public : - wxUITextFieldControl( wxWindow *wxPeer, UITextField* w ); + wxUITextFieldControl( wxTextCtrl *wxPeer, UITextField* w ); virtual ~wxUITextFieldControl(); virtual wxString GetStringValue() const ; @@ -35,9 +35,14 @@ public : virtual void WriteText(const wxString& str) ; virtual bool HasOwnContextMenu() const { return true; } + virtual wxSize GetBestSize() const; + + virtual bool SetHint(const wxString& hint); + virtual void controlAction(WXWidget slf, void* _cmd, void *sender); protected : UITextField* m_textField; + NSObject* m_delegate; long m_selStart; long m_selEnd; }; @@ -71,6 +76,7 @@ public: virtual wxSize GetBestSize() const; protected: + NSObject* m_delegate; UITextView* m_textView; }; diff --git a/include/wx/osx/textctrl.h b/include/wx/osx/textctrl.h index 7386fe2934..283a1694ea 100644 --- a/include/wx/osx/textctrl.h +++ b/include/wx/osx/textctrl.h @@ -79,6 +79,10 @@ public: // in a single line text control virtual void SetMaxLength(unsigned long len); + // set the grayed out hint text + virtual bool SetHint(const wxString& hint); + virtual wxString GetHint() const; + // text control under some platforms supports the text styles: these // methods apply the given text style to the given selection or to // set/get the style which will be used for all appended text @@ -152,6 +156,7 @@ protected: private : wxMenu *m_privateContextMenu; + wxString m_hintString; DECLARE_EVENT_TABLE() }; diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index 6106bedc70..7703e852a6 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -684,6 +684,13 @@ void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), } } +bool wxNSTextFieldControl::SetHint(const wxString& hint) +{ + wxCFStringRef hintstring(hint); + [[m_textField cell] setPlaceholderString:hintstring.AsNSString()]; + return true; +} + // // // diff --git a/src/osx/iphone/textctrl.mm b/src/osx/iphone/textctrl.mm index b1ab63382c..db3cf4b0dc 100644 --- a/src/osx/iphone/textctrl.mm +++ b/src/osx/iphone/textctrl.mm @@ -52,7 +52,7 @@ // currently for some reasong the UITextField leads to a recursion when the keyboard should be shown, so let's leave the code // in case this gets resolved... -#define wxOSX_IPHONE_USE_TEXTFIELD 0 +#define wxOSX_IPHONE_USE_TEXTFIELD 1 class wxMacEditHelper { @@ -81,7 +81,20 @@ protected : #if wxOSX_IPHONE_USE_TEXTFIELD -@interface wxUITextField : UITextField +@interface wxUITextFieldDelegate : NSObject +{ +} + +@end + + +@interface wxUITextField : UITextField +{ +} + +@end + +@interface wxNSSecureTextField : UITextField { } @@ -119,6 +132,7 @@ protected : @end +#if 0 @implementation wxUITextFieldEditor - (void) keyDown:(NSEvent*) event @@ -161,6 +175,45 @@ protected : @end +#endif + + +@implementation wxUITextFieldDelegate + +- (BOOL)textFieldShouldReturn:(UITextField *)textField +{ + // the user pressed the "Done" button, so dismiss the keyboard + wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textField ); + if ( impl ) + { + wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); + if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) + { + wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); + event.SetEventObject( wxpeer ); + event.SetString( static_cast(wxpeer)->GetValue() ); + wxpeer->HandleWindowEvent( event ); + } + } + + [textField resignFirstResponder]; + return YES; +} + +/* +- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. +- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder +- (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 +- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called + +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text + +- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) +*/ + +@end + + @implementation wxUITextField + (void)initialize @@ -187,18 +240,6 @@ protected : { wxUnusedVar(textField); - wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); - if ( impl ) - { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) - { - wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); - event.SetEventObject( wxpeer ); - event.SetString( static_cast(wxpeer)->GetValue() ); - wxpeer->HandleWindowEvent( event ); - } - } return NO; } @@ -276,19 +317,18 @@ wxUITextViewControl::wxUITextViewControl( wxTextCtrl *wxPeer, UITextView* v) : wxTextWidgetImpl(wxPeer) { m_textView = v; - wxUITextViewDelegate* d = [[wxUITextViewDelegate alloc] init]; + m_delegate= [[wxUITextViewDelegate alloc] init]; - [m_textView setDelegate:d]; + [m_textView setDelegate:m_delegate]; } wxUITextViewControl::~wxUITextViewControl() { if (m_textView) { - wxUITextViewDelegate* d = [m_textView delegate]; [m_textView setDelegate: nil]; - [d release]; } + [m_delegate release]; } bool wxUITextViewControl::CanFocus() const @@ -402,9 +442,9 @@ bool wxUITextViewControl::GetStyle(long position, wxTextAttr& style) { if (m_textView && position >=0) { - UIFont* font = NULL; - NSColor* bgcolor = NULL; - NSColor* fgcolor = NULL; + // UIFont* font = NULL; + // NSColor* bgcolor = NULL; + // NSColor* fgcolor = NULL; // NOTE: It appears that other platforms accept GetStyle with the position == length // but that UITextStorage does not accept length as a valid position. // Therefore we return the default control style in that case. @@ -487,7 +527,12 @@ wxSize wxUITextViewControl::GetBestSize() const } return wxSize(0,0); */ - return r.GetSize(); + + wxSize sz = r.GetSize(); + if ( sz.y < 31 ) + sz.y = 31; + + return sz; } #if wxOSX_IPHONE_USE_TEXTFIELD @@ -496,11 +541,13 @@ wxSize wxUITextViewControl::GetBestSize() const // wxUITextFieldControl // -wxUITextFieldControl::wxUITextFieldControl( wxWindow *wxPeer, UITextField* w ) : wxWidgetIPhoneImpl(wxPeer, w) +wxUITextFieldControl::wxUITextFieldControl( wxTextCtrl *wxPeer, UITextField* w ) : + wxWidgetIPhoneImpl(wxPeer, w), + wxTextWidgetImpl(wxPeer) { - UITextField wxOSX_10_6_AND_LATER() *tf = (UITextField*) w; - m_textField = tf; - [m_textField setDelegate: tf]; + m_textField = w; + m_delegate = [[wxUITextFieldDelegate alloc] init]; + [m_textField setDelegate: m_delegate]; m_selStart = m_selEnd = 0; } @@ -508,6 +555,7 @@ wxUITextFieldControl::~wxUITextFieldControl() { if (m_textField) [m_textField setDelegate: nil]; + [m_delegate release]; } wxString wxUITextFieldControl::GetStringValue() const @@ -521,6 +569,17 @@ void wxUITextFieldControl::SetStringValue( const wxString &str) [m_textField setText: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; } +wxSize wxUITextFieldControl::GetBestSize() const +{ + wxRect r; + + GetBestRect(&r); + wxSize sz = r.GetSize(); + if ( sz.y < 31 ) + sz.y = 31; + return sz; +} + void wxUITextFieldControl::Copy() { [m_textField copy:nil]; @@ -611,6 +670,12 @@ void wxUITextFieldControl::controlAction(WXWidget WXUNUSED(slf), } } +bool wxUITextFieldControl::SetHint(const wxString& hint) +{ + wxCFStringRef hintstring(hint); + [m_textField setPlaceholder:hintstring.AsNSString()]; +} + #endif // @@ -646,22 +711,19 @@ wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, #if wxOSX_IPHONE_USE_TEXTFIELD else { - UITextField* v = [[UITextField alloc] initWithFrame:r]; + wxUITextField* v = [[wxUITextField alloc] initWithFrame:r]; tv = v; v.textColor = [UIColor blackColor]; v.font = [UIFont systemFontOfSize:17.0]; - v.placeholder = @""; v.backgroundColor = [UIColor whiteColor]; - v.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right + v.clearButtonMode = UITextFieldViewModeNever; - v.delegate = v; // let us be the delegate so we know when the keyboard's "Done" button is pressed - [v setBorderStyle:UITextBorderStyleBezel]; if ( style & wxNO_BORDER ) v.borderStyle = UITextBorderStyleNone; - + wxUITextFieldControl* tc = new wxUITextFieldControl( wxpeer, v ); c = tc; t = tc; -- 2.45.2