From 6f3f38980f10a935f3b47dbf0d3b4643e96a4be2 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Sun, 2 Jan 2011 10:14:29 +0000 Subject: [PATCH] Added 'HasAlpha' attribute for wxColourProperty. Setting it to true allows user to edit the alpha component. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66520 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/propgrid/advprops.h | 3 +- include/wx/propgrid/property.h | 6 ++++ interface/wx/propgrid/property.h | 9 +++++ samples/propgrid/propgrid.cpp | 8 ++++- samples/propgrid/tests.cpp | 12 +++++++ src/propgrid/advprops.cpp | 57 +++++++++++++++++++++++++++----- 7 files changed, 85 insertions(+), 11 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 87d426564b..db79f3c024 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -451,6 +451,7 @@ All (GUI): - Added wxImage::Rotate180() (Jeff Tupper). - Added support for saving TGA files. - Added wxArtProvider returning higher quality icons from Tango project. +- wxPropertyGrid: Added "HasAlpha" attribute for wxColourProperty. GTK: diff --git a/include/wx/propgrid/advprops.h b/include/wx/propgrid/advprops.h index 34306a3caf..860b89029b 100644 --- a/include/wx/propgrid/advprops.h +++ b/include/wx/propgrid/advprops.h @@ -203,7 +203,8 @@ public: Override in derived class to customize how colours are printed as strings. */ - virtual wxString ColourToString( const wxColour& col, int index ) const; + virtual wxString ColourToString( const wxColour& col, int index, + int argFlags = 0 ) const; /** Returns index of entry that triggers colour picker dialog (default is last). diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index f72e352e8f..dea6f5c63a 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -697,6 +697,12 @@ wxPG_PROP_BEING_DELETED = 0x00200000 */ #define wxPG_COLOUR_ALLOW_CUSTOM wxS("AllowCustom") +/** + wxColourProperty and its kind: Set to True in order to support editing + alpha colour component. +*/ +#define wxPG_COLOUR_HAS_ALPHA wxS("HasAlpha") + /** @} */ diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index f67b50e0da..868b32bbfb 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -173,6 +173,12 @@ */ #define wxPG_COLOUR_ALLOW_CUSTOM wxS("AllowCustom") +/** + wxColourProperty and its kind: Set to True in order to support editing + alpha colour component. +*/ +#define wxPG_COLOUR_HAS_ALPHA wxS("HasAlpha") + /** @} */ @@ -527,6 +533,9 @@ wxPG_PROP_BEING_DELETED = 0x00200000 There are various sub-classing opportunities with this class. See below in wxSystemColourProperty section for details. + Setting "HasAlpha" attribute to @true for this property allows user to + edit the alpha colour component. + @subsection wxFontProperty Represents wxFont. Various sub-properties are used to edit individual diff --git a/samples/propgrid/propgrid.cpp b/samples/propgrid/propgrid.cpp index 22d474fb4e..99586e6e30 100644 --- a/samples/propgrid/propgrid.cpp +++ b/samples/propgrid/propgrid.cpp @@ -1259,13 +1259,19 @@ void FormMain::PopulateWithExamples () #endif pid = pg->Append( new wxColourProperty(wxT("ColourProperty"),wxPG_LABEL,*wxRED) ); - //pg->SetPropertyAttribute(pid,wxPG_COLOUR_ALLOW_CUSTOM,false); pg->SetPropertyEditor( wxT("ColourProperty"), wxPGEditor_ComboBox ); pg->GetProperty(wxT("ColourProperty"))->SetAutoUnspecified(true); pg->SetPropertyHelpString( wxT("ColourProperty"), wxT("wxPropertyGrid::SetPropertyEditor method has been used to change ") wxT("editor of this property to wxPGEditor_ComboBox)")); + pid = pg->Append( new wxColourProperty("ColourPropertyWithAlpha", + wxPG_LABEL, + wxColour(15, 200, 95, 128)) ); + pg->SetPropertyAttribute("ColourPropertyWithAlpha", "HasAlpha", true); + pg->SetPropertyHelpString("ColourPropertyWithAlpha", + "Attribute \"HasAlpha\" is set to true for this property."); + // // This demonstrates using alternative editor for colour property // to trigger colour dialog directly from button. diff --git a/samples/propgrid/tests.cpp b/samples/propgrid/tests.cpp index 9ab65f06ee..331e7e8a09 100644 --- a/samples/propgrid/tests.cpp +++ b/samples/propgrid/tests.cpp @@ -616,6 +616,9 @@ bool FormMain::RunTests( bool fullTest, bool interactive ) dt2.SetYear(dt2.GetYear()-10); #endif + wxColour colWithAlpha(1, 128, 254, 100); + wxString colWithAlphaStr(colWithAlpha.GetAsString(wxC2S_CSS_SYNTAX)); + #define FLAG_TEST_SET1 (wxCAPTION|wxCLOSE_BOX|wxSYSTEM_MENU|wxRESIZE_BORDER) #define FLAG_TEST_SET2 (wxSTAY_ON_TOP|wxCAPTION|wxICONIZE|wxSYSTEM_MENU) @@ -762,6 +765,15 @@ bool FormMain::RunTests( bool fullTest, bool interactive ) col << pgman->GetPropertyValue("ColourProperty"); if ( col != wxColour(254, 134, 11) ) RT_FAILURE(); + + pgman->SetPropertyValueString("ColourPropertyWithAlpha", + "(10, 20, 30, 128)"); + col << pgman->GetPropertyValue("ColourPropertyWithAlpha"); + if ( col != wxColour(10, 20, 30, 128) ) + RT_FAILURE(); + if ( pgman->GetPropertyValueAsString("ColourPropertyWithAlpha") + != "(10,20,30,128)" ) + RT_FAILURE(); } { diff --git a/src/propgrid/advprops.cpp b/src/propgrid/advprops.cpp index b29d63a48c..2d3a67f101 100644 --- a/src/propgrid/advprops.cpp +++ b/src/propgrid/advprops.cpp @@ -1143,15 +1143,35 @@ wxColour wxSystemColourProperty::GetColour( int index ) const return wxSystemSettings::GetColour( (wxSystemColour)index ); } -wxString wxSystemColourProperty::ColourToString( const wxColour& col, int index ) const +wxString wxSystemColourProperty::ColourToString( const wxColour& col, + int index, + int argFlags ) const { + if ( index == wxNOT_FOUND ) - return wxString::Format(wxT("(%i,%i,%i)"), - (int)col.Red(), - (int)col.Green(), - (int)col.Blue()); + { + + if ( (argFlags & wxPG_FULL_VALUE) || + GetAttributeAsLong(wxPG_COLOUR_HAS_ALPHA, 0) ) + { + return wxString::Format(wxS("(%i,%i,%i,%i)"), + (int)col.Red(), + (int)col.Green(), + (int)col.Blue(), + (int)col.Alpha()); + } + else + { + return wxString::Format(wxS("(%i,%i,%i)"), + (int)col.Red(), + (int)col.Green(), + (int)col.Blue()); + } + } else + { return m_choices.GetLabel(index); + } } wxString wxSystemColourProperty::ValueToString( wxVariant& value, @@ -1178,7 +1198,7 @@ wxString wxSystemColourProperty::ValueToString( wxVariant& value, index = m_choices.Index(val.m_type); } - return ColourToString(val.m_colour, index); + return ColourToString(val.m_colour, index, argFlags); } @@ -1383,13 +1403,32 @@ bool wxSystemColourProperty::StringToValue( wxVariant& value, const wxString& te if ( colStr != custColName ) { - // Convert (R,G,B) to rgb(R,G,B) if ( colStr.Find(wxS("(")) == 0 ) { - colStr = wxS("rgb") + colStr; + // Eliminate whitespace + colStr.Replace(wxS(" "), wxEmptyString); + + int commaCount = colStr.Freq(wxS(',')); + if ( commaCount == 2 ) + { + // Convert (R,G,B) to rgb(R,G,B) + colStr = wxS("rgb") + colStr; + } + else if ( commaCount == 3 ) + { + // We have int alpha, CSS format that wxColour takes as + // input processes float alpha. So, let's parse the colour + // ourselves instead of trying to convert it to a format + // that wxColour::FromString() understands. + int r = -1, g = -1, b = -1, a = -1; + wxSscanf(colStr, wxS("(%i,%i,%i,%i)"), &r, &g, &b, &a); + customColour.Set(r, g, b, a); + conversionSuccess = customColour.IsOk(); + } } - conversionSuccess = customColour.Set(colStr); + if ( !conversionSuccess ) + conversionSuccess = customColour.Set(colStr); } if ( !conversionSuccess && m_choices.GetCount() && -- 2.45.2