From: Jaakko Salli Date: Sat, 20 Nov 2010 11:58:54 +0000 (+0000) Subject: Reworked wxSystemColourProperty::StringToValue() to use wxColour::Set() instead of... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/780cccd730e73416229d87c2d437839b597d3cb7 Reworked wxSystemColourProperty::StringToValue() to use wxColour::Set() instead of doing string-to-colour conversion by itself. This adds support for HTML-colours, among other things (closes #12696). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66217 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/samples/propgrid/tests.cpp b/samples/propgrid/tests.cpp index 5d62dbfe81..3eafa29d97 100644 --- a/samples/propgrid/tests.cpp +++ b/samples/propgrid/tests.cpp @@ -752,6 +752,16 @@ bool FormMain::RunTests( bool fullTest, bool interactive ) wxLogDebug("Did not match: Car.Convertible=%s", pgman->GetPropertyValueAsString("Car.Convertible").c_str()); RT_FAILURE(); } + + // SetPropertyValueString for special cases such as wxColour + pgman->SetPropertyValueString("ColourProperty", "(123,4,255)"); + col << pgman->GetPropertyValue("ColourProperty"); + if ( col != wxColour(123, 4, 255) ) + RT_FAILURE(); + pgman->SetPropertyValueString("ColourProperty", "#FE860B"); + col << pgman->GetPropertyValue("ColourProperty"); + if ( col != wxColour(254, 134, 11) ) + RT_FAILURE(); } { diff --git a/src/propgrid/advprops.cpp b/src/propgrid/advprops.cpp index dafde5ff54..b29d63a48c 100644 --- a/src/propgrid/advprops.cpp +++ b/src/propgrid/advprops.cpp @@ -1373,37 +1373,28 @@ void wxSystemColourProperty::OnCustomPaint( wxDC& dc, const wxRect& rect, bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& text, int argFlags ) const { - // - // Accept colour format "[Name] [(R,G,B)]" - // Name takes precedence. - // - wxString colourName; - wxString colourRGB; + wxString custColName(m_choices.GetLabel(GetCustomColourIndex())); + wxString colStr(text); + colStr.Trim(true); + colStr.Trim(false); - int ppos = text.Find(wxT("(")); + wxColour customColour; + bool conversionSuccess = false; - if ( ppos == wxNOT_FOUND ) - { - colourName = text; - } - else + if ( colStr != custColName ) { - colourName = text.substr(0, ppos); - colourRGB = text.substr(ppos, text.length()-ppos); - } - - // Strip spaces from extremities - colourName.Trim(true); - colourName.Trim(false); - colourRGB.Trim(true); + // Convert (R,G,B) to rgb(R,G,B) + if ( colStr.Find(wxS("(")) == 0 ) + { + colStr = wxS("rgb") + colStr; + } - // Validate colourRGB string - (1,1,1) is shortest allowed - if ( colourRGB.length() < 7 ) - colourRGB.clear(); + conversionSuccess = customColour.Set(colStr); + } - if ( colourRGB.length() == 0 && m_choices.GetCount() && + if ( !conversionSuccess && m_choices.GetCount() && !(m_flags & wxPG_PROP_HIDE_CUSTOM_COLOUR) && - colourName == m_choices.GetLabel(GetCustomColourIndex()) ) + colStr == custColName ) { if ( !(argFlags & wxPG_EDITABLE_VALUE )) { @@ -1421,10 +1412,12 @@ bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& te bool done = false; - if ( colourName.length() ) + if ( !conversionSuccess ) { // Try predefined colour first - bool res = wxEnumProperty::StringToValue(value, colourName, argFlags); + bool res = wxEnumProperty::StringToValue(value, + colStr, + argFlags); if ( res && GetIndex() >= 0 ) { val.m_type = GetIndex(); @@ -1437,22 +1430,11 @@ bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& te done = true; } } - if ( colourRGB.length() && !done ) + else { - // Then check custom colour. val.m_type = wxPG_COLOUR_CUSTOM; - - int r = -1, g = -1, b = -1; - wxSscanf(colourRGB.c_str(),wxT("(%i,%i,%i)"),&r,&g,&b); - - if ( r >= 0 && r <= 255 && - g >= 0 && g <= 255 && - b >= 0 && b <= 255 ) - { - val.m_colour.Set(r,g,b); - - done = true; - } + val.m_colour = customColour; + done = true; } if ( !done )