Fix a hack implemented in wxEnumProperty::ValueFromString_ and wxEnumProperty::ValueFromInt_ functions.

wxEnumProperty code is refactored in order to fix a hack which purpose was (apparently) to bypass constness of these functions by caching determined indices in wxEnumProperty::ms_nextIndex static member variable for further processing. (Unclear concept of using this static member was referred in http://trac.wxwidgets.org/ticket/12779#comment:9)
Now, determined index is returned to the caller and processed there if necessary and hence caching of this index is not necessary.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@78390 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Artur Wieczorek 2015-01-19 17:22:29 +00:00
parent eec7ef7232
commit 752938cecd
3 changed files with 51 additions and 42 deletions

View File

@ -454,20 +454,34 @@ protected:
int GetIndex() const;
void SetIndex( int index );
#if WXWIN_COMPATIBILITY_3_0
wxDEPRECATED_MSG("use ValueFromString_(wxVariant&, int*, const wxString&, int) function instead")
bool ValueFromString_( wxVariant& value,
const wxString& text,
int argFlags ) const;
bool ValueFromInt_( wxVariant& value, int intVal, int argFlags ) const;
static void ResetNextIndex() { ms_nextIndex = -2; }
int argFlags ) const
{
return ValueFromString_(value, NULL, text, argFlags);
}
wxDEPRECATED_MSG("use ValueFromInt_(wxVariant&, int*, int, int) function instead")
bool ValueFromInt_( wxVariant& value, int intVal, int argFlags ) const
{
return ValueFromInt_(value, NULL, intVal, argFlags);
}
wxDEPRECATED_MSG("don't use ResetNextIndex() function")
static void ResetNextIndex() { }
#endif
// Converts text to value and returns corresponding index in the collection
bool ValueFromString_(wxVariant& value,
int* pIndex,
const wxString& text,
int argFlags) const;
// Converts number to value and returns corresponding index in the collection
bool ValueFromInt_(wxVariant& value, int* pIndex, int intVal, int argFlags) const;
private:
// This is private so that classes are guaranteed to use GetIndex
// for up-to-date index value.
int m_index;
// Relies on ValidateValue being called always before OnSetValue
static int ms_nextIndex;
};
// -----------------------------------------------------------------------

View File

@ -1454,13 +1454,11 @@ bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& te
{
// This really should not occur...
// wxASSERT(false);
ResetNextIndex();
return false;
}
if ( !QueryColourFromUser(value) )
{
ResetNextIndex();
if ( !(argFlags & wxPG_PROPERTY_SPECIFIC) )
return false;
// If query for value comes from the event handler
@ -1477,12 +1475,11 @@ bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& te
if ( !conversionSuccess )
{
// Try predefined colour first
bool res = wxEnumProperty::StringToValue(value,
colStr,
argFlags);
if ( res && GetIndex() >= 0 )
int index;
bool res = ValueFromString_(value, &index, colStr, argFlags);
if ( res && index >= 0 )
{
val.m_type = GetIndex();
val.m_type = index;
if ( val.m_type < m_choices.GetCount() )
val.m_type = m_choices[val.m_type].GetValue();
@ -1501,7 +1498,6 @@ bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& te
if ( !done )
{
ResetNextIndex();
return false;
}

View File

@ -1200,29 +1200,27 @@ wxEnumProperty::~wxEnumProperty ()
{
}
int wxEnumProperty::ms_nextIndex = -2;
void wxEnumProperty::OnSetValue()
{
wxString variantType = m_value.GetType();
int index = -2;
if ( variantType == wxPG_VARIANT_TYPE_LONG )
{
ValueFromInt_( m_value, m_value.GetLong(), wxPG_FULL_VALUE );
ValueFromInt_(m_value, &index, m_value.GetLong(), wxPG_FULL_VALUE);
}
else if ( variantType == wxPG_VARIANT_TYPE_STRING )
{
ValueFromString_( m_value, m_value.GetString(), 0 );
ValueFromString_(m_value, &index, m_value.GetString(), 0);
}
else
{
wxFAIL;
}
if ( ms_nextIndex != -2 )
if (index != -2)
{
m_index = ms_nextIndex;
ms_nextIndex = -2;
m_index = index;
}
}
@ -1233,7 +1231,7 @@ bool wxEnumProperty::ValidateValue( wxVariant& value, wxPGValidationInfo& WXUNUS
// To reduce code size, use conversion here as well
if ( value.GetType() == wxPG_VARIANT_TYPE_STRING &&
!wxDynamicCastThis(wxEditEnumProperty) )
return ValueFromString_( value, value.GetString(), wxPG_PROPERTY_SPECIFIC );
return ValueFromString_(value, NULL, value.GetString(), wxPG_PROPERTY_SPECIFIC);
return true;
}
@ -1253,15 +1251,15 @@ wxString wxEnumProperty::ValueToString( wxVariant& value,
bool wxEnumProperty::StringToValue( wxVariant& variant, const wxString& text, int argFlags ) const
{
return ValueFromString_( variant, text, argFlags );
return ValueFromString_(variant, NULL, text, argFlags);
}
bool wxEnumProperty::IntToValue( wxVariant& variant, int intVal, int argFlags ) const
{
return ValueFromInt_( variant, intVal, argFlags );
return ValueFromInt_(variant, NULL, intVal, argFlags);
}
bool wxEnumProperty::ValueFromString_( wxVariant& value, const wxString& text, int argFlags ) const
bool wxEnumProperty::ValueFromString_(wxVariant& value, int* pIndex, const wxString& text, int WXUNUSED(argFlags)) const
{
int useIndex = -1;
long useValue = 0;
@ -1311,20 +1309,24 @@ bool wxEnumProperty::ValueFromString_( wxVariant& value, const wxString& text, i
if ( setAsNextIndex != -2 )
{
// If wxPG_PROPERTY_SPECIFIC is set, then this is done for
// validation purposes only, and index must not be changed
if ( !(argFlags & wxPG_PROPERTY_SPECIFIC) )
ms_nextIndex = setAsNextIndex;
if (pIndex)
{
*pIndex = setAsNextIndex;
}
if ( isEdit || setAsNextIndex != -1 )
return true;
else
return false;
}
if (pIndex)
{
*pIndex = useIndex;
}
return false;
}
bool wxEnumProperty::ValueFromInt_( wxVariant& variant, int intVal, int argFlags ) const
bool wxEnumProperty::ValueFromInt_( wxVariant& variant, int* pIndex, int intVal, int argFlags ) const
{
// If wxPG_FULL_VALUE is *not* in argFlags, then intVal is index from combo box.
//
@ -1344,32 +1346,32 @@ bool wxEnumProperty::ValueFromInt_( wxVariant& variant, int intVal, int argFlags
if ( setAsNextIndex != -2 )
{
// If wxPG_PROPERTY_SPECIFIC is set, then this is done for
// validation or fetching a data purposes only, and index must not be changed.
if ( !(argFlags & wxPG_PROPERTY_SPECIFIC) )
ms_nextIndex = setAsNextIndex;
if ( !(argFlags & wxPG_FULL_VALUE) )
intVal = m_choices.GetValue(intVal);
variant = (long)intVal;
if (pIndex)
{
*pIndex = setAsNextIndex;
}
return true;
}
if (pIndex)
{
*pIndex = intVal;
}
return false;
}
void
wxEnumProperty::OnValidationFailure( wxVariant& WXUNUSED(pendingValue) )
{
// Revert index
ResetNextIndex();
}
void wxEnumProperty::SetIndex( int index )
{
ms_nextIndex = -2;
m_index = index;
}
@ -1378,9 +1380,6 @@ int wxEnumProperty::GetIndex() const
if ( m_value.IsNull() )
return -1;
if ( ms_nextIndex != -2 )
return ms_nextIndex;
return m_index;
}