From 238b33ab0e8c53a871998f385e957313eef2d804 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Mon, 20 Dec 2010 13:51:24 +0000 Subject: [PATCH] Added wxComboPopup::FindItem() to help in deciding how SetValue() should change the value of a read-only wxComboCtrl. This allows wxOwnerDrawnComboBox to have the same behavior as wxComboBox in that respect. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66409 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/combo.h | 7 +++++++ include/wx/odcombo.h | 1 + interface/wx/combo.h | 21 ++++++++++++++++++++- src/common/combocmn.cpp | 28 ++++++++++++++++++++++++---- src/generic/odcombo.cpp | 10 ++++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/include/wx/combo.h b/include/wx/combo.h index 0a9359ac36..9c38cda211 100644 --- a/include/wx/combo.h +++ b/include/wx/combo.h @@ -774,6 +774,13 @@ public: // Gets displayed string representation of the value. virtual wxString GetStringValue() const = 0; + // Called to check if the popup - when an item container - actually + // has matching item. Case-sensitivity checking etc. is up to the + // implementation. If the found item matched the string, but is + // different, it should be written back to pItem. Default implementation + // always return true and does not alter trueItem. + virtual bool FindItem(const wxString& item, wxString* trueItem=NULL); + // This is called to custom paint in the combo control itself (ie. not the popup). // Default implementation draws value as string. virtual void PaintComboControl( wxDC& dc, const wxRect& rect ); diff --git a/include/wx/odcombo.h b/include/wx/odcombo.h index 706d04e2ba..73a954c250 100644 --- a/include/wx/odcombo.h +++ b/include/wx/odcombo.h @@ -93,6 +93,7 @@ public: virtual void OnComboCharEvent( wxKeyEvent& event ); virtual void OnComboDoubleClick(); virtual bool LazyCreate(); + virtual bool FindItem(const wxString& item, wxString* trueItem); // Item management void SetSelection( int item ); diff --git a/interface/wx/combo.h b/interface/wx/combo.h index 6574deed34..af438253f3 100644 --- a/interface/wx/combo.h +++ b/interface/wx/combo.h @@ -41,6 +41,25 @@ public: */ void Dismiss(); + /** + Implement to customize matching of value string to an item container + entry. + + @param item + String entered, usually by user or from SetValue() call. + + @param trueItem + When item matches an entry, but the entry's string representation + is not exactly the same (case mismatch, for example), then the + true item string should be written back to here, if it is not + a NULL pointer. + + @remarks + Default implementation always return true and does not alter + trueItem. + */ + virtual bool FindItem(const wxString& item, wxString* trueItem=NULL); + /** The derived class may implement this to return adjusted size for the popup control, according to the variables given. @@ -86,7 +105,7 @@ public: Useful in conjunction with LazyCreate(). */ bool IsCreated() const; - + /** The derived class may implement this to return @true if it wants to delay call to Create() until the popup is shown for the first time. It diff --git a/src/common/combocmn.cpp b/src/common/combocmn.cpp index 53f8b30849..0b3a518a1b 100644 --- a/src/common/combocmn.cpp +++ b/src/common/combocmn.cpp @@ -590,6 +590,12 @@ void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) ) { } +bool wxComboPopup::FindItem(const wxString& WXUNUSED(item), + wxString* WXUNUSED(trueItem)) +{ + return true; +} + bool wxComboPopup::LazyCreate() { return false; @@ -2575,12 +2581,26 @@ void wxComboCtrlBase::OnSetValue(const wxString& value) // to set the string value here (as well as sometimes in ShowPopup). if ( m_valueString != value ) { - m_valueString = value; + bool found = true; + wxString trueValue = value; + + // Conform to wxComboBox behavior: read-only control can only accept + // valid list items and empty string + if ( m_popupInterface && HasFlag(wxCB_READONLY) && value.length() ) + { + found = m_popupInterface->FindItem(value, + &trueValue); + } - EnsurePopupControl(); + if ( found ) + { + m_valueString = trueValue; - if (m_popupInterface) - m_popupInterface->SetStringValue(value); + EnsurePopupControl(); + + if ( m_popupInterface ) + m_popupInterface->SetStringValue(trueValue); + } } Refresh(); diff --git a/src/generic/odcombo.cpp b/src/generic/odcombo.cpp index 7321ec28d8..da2fa78027 100644 --- a/src/generic/odcombo.cpp +++ b/src/generic/odcombo.cpp @@ -642,6 +642,16 @@ int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const return m_strings.Index(s, bCase); } +bool wxVListBoxComboPopup::FindItem(const wxString& item, wxString* trueItem) +{ + int idx = m_strings.Index(item, false); + if ( idx == wxNOT_FOUND ) + return false; + if ( trueItem != NULL ) + *trueItem = m_strings[idx]; + return true; +} + unsigned int wxVListBoxComboPopup::GetCount() const { return m_strings.GetCount(); -- 2.47.2