From: Jaakko Salli Date: Sat, 26 Dec 2009 10:33:35 +0000 (+0000) Subject: Added wxComboCtrl::SetHint(), GetHint() X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/107defe36a68091303be9a11c8abb83432acee8c?ds=inline Added wxComboCtrl::SetHint(), GetHint() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62989 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/combo.h b/include/wx/combo.h index 718adde355..2cdb81102a 100644 --- a/include/wx/combo.h +++ b/include/wx/combo.h @@ -385,6 +385,10 @@ public: const wxBitmap& GetBitmapHover() const { return m_bmpHover; } const wxBitmap& GetBitmapDisabled() const { return m_bmpDisabled; } + // Hint functions mirrored from TextEntryBase + virtual bool SetHint(const wxString& hint); + virtual wxString GetHint() const; + // Margins functions mirrored from TextEntryBase // (wxComboCtrl does not inherit from wxTextEntry, but may embed a // wxTextCtrl, so we need these). Also note that these functions @@ -427,6 +431,16 @@ public: protected: + // Returns true if hint text should be drawn in the control + bool ShouldUseHintText(int flags = 0) const + { + return ( !m_text && + !(flags & wxCONTROL_ISSUBMENU) && + !m_valueString.length() && + m_hintText.length() && + !ShouldDrawFocus() ); + } + // // Override these for customization purposes // @@ -544,6 +558,9 @@ protected: // This is used when m_text is hidden (readonly). wxString m_valueString; + // This is used when control is unfocused and m_valueString is empty + wxString m_hintText; + // the text control and button we show all the time wxTextCtrl* m_text; wxWindow* m_btn; diff --git a/interface/wx/combo.h b/interface/wx/combo.h index fc30d8c4c9..4a67cd784d 100644 --- a/interface/wx/combo.h +++ b/interface/wx/combo.h @@ -448,6 +448,15 @@ public: */ static int GetFeatures(); + /** + Returns the current hint string. + + See SetHint() for more information about hints. + + @since 2.9.1 + */ + virtual wxString GetHint() const; + /** Returns the insertion point for the combo control's text field. @@ -620,6 +629,18 @@ public: */ void SetCustomPaintWidth(int width); + /** + Sets a hint shown in an empty unfocused combo control. + + Notice that hints are known as cue banners under MSW or + placeholder strings under OS X. + + @see wxTextEntry::SetHint() + + @since 2.9.1 + */ + virtual void SetHint(const wxString& hint); + /** Sets the insertion point in the text field. diff --git a/src/common/combocmn.cpp b/src/common/combocmn.cpp index 10f41f8473..b45e28a2c6 100644 --- a/src/common/combocmn.cpp +++ b/src/common/combocmn.cpp @@ -904,6 +904,7 @@ wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator) m_text->Create(this, wxID_ANY, m_valueString, wxDefaultPosition, wxSize(10,-1), style, validator); + m_text->SetHint(m_hintText); } } @@ -2539,4 +2540,19 @@ void wxComboCtrlBase::Undo() m_text->Undo(); } +bool wxComboCtrlBase::SetHint(const wxString& hint) +{ + m_hintText = hint; + bool res = true; + if ( m_text ) + res = m_text->SetHint(hint); + Refresh(); + return res; +} + +wxString wxComboCtrlBase::GetHint() const +{ + return m_hintText; +} + #endif // wxUSE_COMBOCTRL diff --git a/src/generic/odcombo.cpp b/src/generic/odcombo.cpp index 83dc7cbb95..e70047106d 100644 --- a/src/generic/odcombo.cpp +++ b/src/generic/odcombo.cpp @@ -1089,7 +1089,20 @@ void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc, { if ( flags & wxODCB_PAINTING_CONTROL ) { - dc.DrawText( GetValue(), + wxString text; + + if ( !ShouldUseHintText() ) + { + text = GetValue(); + } + else + { + text = GetHint(); + wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT); + dc.SetTextForeground(col); + } + + dc.DrawText( text, rect.x + GetMargins().x, (rect.height-dc.GetCharHeight())/2 + rect.y ); }