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
This commit is contained in:
Stefan Csomor
2011-01-26 15:51:39 +00:00
parent 01470e3741
commit 99eb484a70
6 changed files with 115 additions and 33 deletions

View File

@@ -39,6 +39,7 @@ public :
virtual void SetSelection( long from , long to ); virtual void SetSelection( long from , long to );
virtual void WriteText(const wxString& str) ; virtual void WriteText(const wxString& str) ;
virtual bool HasOwnContextMenu() const { return true; } virtual bool HasOwnContextMenu() const { return true; }
virtual bool SetHint(const wxString& hint);
virtual void controlAction(WXWidget slf, void* _cmd, void *sender); virtual void controlAction(WXWidget slf, void* _cmd, void *sender);

View File

@@ -648,6 +648,7 @@ public :
virtual wxSize GetBestSize() const { return wxDefaultSize; } virtual wxSize GetBestSize() const { return wxDefaultSize; }
virtual bool SetHint(const wxString& WXUNUSED(hint)) { return false; }
private: private:
wxTextEntry * const m_entry; wxTextEntry * const m_entry;

View File

@@ -20,7 +20,7 @@
class wxUITextFieldControl : public wxWidgetIPhoneImpl, public wxTextWidgetImpl class wxUITextFieldControl : public wxWidgetIPhoneImpl, public wxTextWidgetImpl
{ {
public : public :
wxUITextFieldControl( wxWindow *wxPeer, UITextField* w ); wxUITextFieldControl( wxTextCtrl *wxPeer, UITextField* w );
virtual ~wxUITextFieldControl(); virtual ~wxUITextFieldControl();
virtual wxString GetStringValue() const ; virtual wxString GetStringValue() const ;
@@ -35,9 +35,14 @@ public :
virtual void WriteText(const wxString& str) ; virtual void WriteText(const wxString& str) ;
virtual bool HasOwnContextMenu() const { return true; } 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); virtual void controlAction(WXWidget slf, void* _cmd, void *sender);
protected : protected :
UITextField* m_textField; UITextField* m_textField;
NSObject<UITextFieldDelegate>* m_delegate;
long m_selStart; long m_selStart;
long m_selEnd; long m_selEnd;
}; };
@@ -71,6 +76,7 @@ public:
virtual wxSize GetBestSize() const; virtual wxSize GetBestSize() const;
protected: protected:
NSObject<UITextViewDelegate>* m_delegate;
UITextView* m_textView; UITextView* m_textView;
}; };

View File

@@ -79,6 +79,10 @@ public:
// in a single line text control // in a single line text control
virtual void SetMaxLength(unsigned long len); 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 // text control under some platforms supports the text styles: these
// methods apply the given text style to the given selection or to // methods apply the given text style to the given selection or to
// set/get the style which will be used for all appended text // set/get the style which will be used for all appended text
@@ -152,6 +156,7 @@ protected:
private : private :
wxMenu *m_privateContextMenu; wxMenu *m_privateContextMenu;
wxString m_hintString;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@@ -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;
}
// //
// //
// //

View File

@@ -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 // 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... // in case this gets resolved...
#define wxOSX_IPHONE_USE_TEXTFIELD 0 #define wxOSX_IPHONE_USE_TEXTFIELD 1
class wxMacEditHelper class wxMacEditHelper
{ {
@@ -81,7 +81,20 @@ protected :
#if wxOSX_IPHONE_USE_TEXTFIELD #if wxOSX_IPHONE_USE_TEXTFIELD
@interface wxUITextField : UITextField<UITextFieldDelegate> @interface wxUITextFieldDelegate : NSObject<UITextFieldDelegate>
{
}
@end
@interface wxUITextField : UITextField
{
}
@end
@interface wxNSSecureTextField : UITextField<UITextFieldDelegate>
{ {
} }
@@ -119,6 +132,7 @@ protected :
@end @end
#if 0
@implementation wxUITextFieldEditor @implementation wxUITextFieldEditor
- (void) keyDown:(NSEvent*) event - (void) keyDown:(NSEvent*) event
@@ -161,6 +175,45 @@ protected :
@end @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<wxTextCtrl*>(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 @implementation wxUITextField
+ (void)initialize + (void)initialize
@@ -187,18 +240,6 @@ protected :
{ {
wxUnusedVar(textField); 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<wxTextCtrl*>(wxpeer)->GetValue() );
wxpeer->HandleWindowEvent( event );
}
}
return NO; return NO;
} }
@@ -276,19 +317,18 @@ wxUITextViewControl::wxUITextViewControl( wxTextCtrl *wxPeer, UITextView* v) :
wxTextWidgetImpl(wxPeer) wxTextWidgetImpl(wxPeer)
{ {
m_textView = v; m_textView = v;
wxUITextViewDelegate* d = [[wxUITextViewDelegate alloc] init]; m_delegate= [[wxUITextViewDelegate alloc] init];
[m_textView setDelegate:d]; [m_textView setDelegate:m_delegate];
} }
wxUITextViewControl::~wxUITextViewControl() wxUITextViewControl::~wxUITextViewControl()
{ {
if (m_textView) if (m_textView)
{ {
wxUITextViewDelegate* d = [m_textView delegate];
[m_textView setDelegate: nil]; [m_textView setDelegate: nil];
[d release];
} }
[m_delegate release];
} }
bool wxUITextViewControl::CanFocus() const bool wxUITextViewControl::CanFocus() const
@@ -402,9 +442,9 @@ bool wxUITextViewControl::GetStyle(long position, wxTextAttr& style)
{ {
if (m_textView && position >=0) if (m_textView && position >=0)
{ {
UIFont* font = NULL; // UIFont* font = NULL;
NSColor* bgcolor = NULL; // NSColor* bgcolor = NULL;
NSColor* fgcolor = NULL; // NSColor* fgcolor = NULL;
// NOTE: It appears that other platforms accept GetStyle with the position == length // NOTE: It appears that other platforms accept GetStyle with the position == length
// but that UITextStorage does not accept length as a valid position. // but that UITextStorage does not accept length as a valid position.
// Therefore we return the default control style in that case. // Therefore we return the default control style in that case.
@@ -487,7 +527,12 @@ wxSize wxUITextViewControl::GetBestSize() const
} }
return wxSize(0,0); 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 #if wxOSX_IPHONE_USE_TEXTFIELD
@@ -496,11 +541,13 @@ wxSize wxUITextViewControl::GetBestSize() const
// wxUITextFieldControl // 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(<UITextFieldDelegate>) *tf = (UITextField*) w; m_textField = w;
m_textField = tf; m_delegate = [[wxUITextFieldDelegate alloc] init];
[m_textField setDelegate: tf]; [m_textField setDelegate: m_delegate];
m_selStart = m_selEnd = 0; m_selStart = m_selEnd = 0;
} }
@@ -508,6 +555,7 @@ wxUITextFieldControl::~wxUITextFieldControl()
{ {
if (m_textField) if (m_textField)
[m_textField setDelegate: nil]; [m_textField setDelegate: nil];
[m_delegate release];
} }
wxString wxUITextFieldControl::GetStringValue() const wxString wxUITextFieldControl::GetStringValue() const
@@ -521,6 +569,17 @@ void wxUITextFieldControl::SetStringValue( const wxString &str)
[m_textField setText: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; [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() void wxUITextFieldControl::Copy()
{ {
[m_textField copy:nil]; [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 #endif
// //
@@ -646,17 +711,14 @@ wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
#if wxOSX_IPHONE_USE_TEXTFIELD #if wxOSX_IPHONE_USE_TEXTFIELD
else else
{ {
UITextField* v = [[UITextField alloc] initWithFrame:r]; wxUITextField* v = [[wxUITextField alloc] initWithFrame:r];
tv = v; tv = v;
v.textColor = [UIColor blackColor]; v.textColor = [UIColor blackColor];
v.font = [UIFont systemFontOfSize:17.0]; v.font = [UIFont systemFontOfSize:17.0];
v.placeholder = @"<enter text>";
v.backgroundColor = [UIColor whiteColor]; 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]; [v setBorderStyle:UITextBorderStyleBezel];
if ( style & wxNO_BORDER ) if ( style & wxNO_BORDER )