From 525b29124a9b69837c1d7973d44da21d2da28ea1 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Sun, 11 Jul 2010 17:04:23 +0000 Subject: [PATCH] Added 'Delimiter' attribute for wxArrayStringProperty. Moved static ArrayStringToString() to wxArrayStringProperty. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64894 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/propgrid/property.h | 9 +++ include/wx/propgrid/propgrid.h | 12 ---- include/wx/propgrid/props.h | 54 ++++++++-------- interface/wx/propgrid/property.h | 9 +++ samples/propgrid/sampleprops.cpp | 3 +- src/propgrid/props.cpp | 102 ++++++++++++++++++++++--------- 7 files changed, 118 insertions(+), 72 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index db6210764c..7c1ce7790b 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -500,6 +500,7 @@ All (GUI): new flags: wxPG_VFB_SHOW_MESSAGEBOX and wxPG_VFB_SHOW_MESSAGE_ON_STATUSBAR. - wxPropertyGrid: Added wxPropertyGrid::DedicateKey(). - wxPropertyGrid: wxArrayStringProperty now uses wxEditableListBox. +- wxPropertyGrid: Added "Delimiter" attribute for wxArrayStringProperty. - wxPropertyGridManager: added wxPG_NO_INTERNAL_BORDER, wxPG_EX_NO_TOOLBAR_DIVIDER and wxPG_EX_TOOLBAR_SEPARATOR styles for finer control over borders. Borders around property grid are now native for diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index f4ddde3f80..e733e5a987 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -644,6 +644,15 @@ wxPG_PROP_BEING_DELETED = 0x00200000 */ #define wxPG_DIR_DIALOG_MESSAGE wxS("DialogMessage") +/** + wxArrayStringProperty's string delimiter character. If this is aquotation + mark or hyphen, then strings will be quoted instead (with given + character). + + Default delimiter is quotation mark. +*/ +#define wxPG_ARRAY_DELIMITER wxS("Delimiter") + /** Sets displayed date format for wxDateProperty. */ #define wxPG_DATE_FORMAT wxS("DateFormat") diff --git a/include/wx/propgrid/propgrid.h b/include/wx/propgrid/propgrid.h index dfa37fca36..83c748da39 100644 --- a/include/wx/propgrid/propgrid.h +++ b/include/wx/propgrid/propgrid.h @@ -1520,18 +1520,6 @@ public: // Events from editor controls are forward to this function void HandleCustomEditorEvent( wxEvent &event ); - /** - Generates contents for string dst based on the contents of - wxArrayString src. - - Format will be "(preDelim)str1(postDelim) (preDelim)str2(postDelim) and - so on. Set flags to 1 inorder to convert backslashes to double-back- - slashes and "(preDelims)"'s to "(preDelims)". - */ - static void ArrayStringToString( wxString& dst, const wxArrayString& src, - wxChar preDelim, wxChar postDelim, - int flags ); - // Mostly useful for page switching. void SwitchState( wxPropertyGridPageState* pNewState ); diff --git a/include/wx/propgrid/props.h b/include/wx/propgrid/props.h index 5eeb376e4f..b2bf59f6e6 100644 --- a/include/wx/propgrid/props.h +++ b/include/wx/propgrid/props.h @@ -716,7 +716,6 @@ class WXDLLIMPEXP_PROPGRID wxArrayStringProperty : public wxPGProperty { WX_PG_DECLARE_PROPERTY_CLASS(wxArrayStringProperty) public: - wxArrayStringProperty( const wxString& label = wxPG_LABEL, const wxString& name = wxPG_LABEL, const wxArrayString& value = wxArrayString() ); @@ -729,8 +728,12 @@ public: int argFlags = 0 ) const; virtual bool OnEvent( wxPropertyGrid* propgrid, wxWindow* primary, wxEvent& event ); + virtual bool DoSetAttribute( const wxString& name, wxVariant& value ); - virtual void GenerateValueAsString(); + // Implement in derived class for custom array-to-string conversion. + virtual void ConvertArrayToString(const wxArrayString& arr, + wxString* pString, + const wxUniChar& delimiter) const; // Shows string editor dialog. Value to be edited should be read from // value, and if dialog is not cancelled, it should be stored back and true @@ -745,8 +748,27 @@ public: // Creates wxPGArrayEditorDialog for string editing. Called in OnButtonClick. virtual wxPGArrayEditorDialog* CreateEditorDialog(); + enum ConversionFlags + { + Escape = 0x01, + QuoteStrings = 0x02 + }; + + /** + Generates contents for string dst based on the contents of + wxArrayString src. + */ + static void ArrayStringToString( wxString& dst, const wxArrayString& src, + wxUniChar delimiter, int flags ); + protected: + // Previously this was to be implemented in derived class for array-to- + // string conversion. Now you should implement ConvertValueToString() + // instead. + virtual void GenerateValueAsString(); + wxString m_display; // Cache for displayed text. + wxUniChar m_delimiter; }; // ----------------------------------------------------------------------- @@ -761,9 +783,6 @@ public: \ const wxString& name = wxPG_LABEL, \ const wxArrayString& value = wxArrayString() ); \ ~PROPNAME(); \ - virtual void GenerateValueAsString(); \ - virtual bool StringToValue( wxVariant& value, \ - const wxString& text, int = 0 ) const; \ virtual bool OnEvent( wxPropertyGrid* propgrid, \ wxWindow* primary, wxEvent& event ); \ virtual bool OnCustomStringEdit( wxWindow* parent, wxString& value ); \ @@ -785,32 +804,9 @@ PROPNAME::PROPNAME( const wxString& label, \ : wxArrayStringProperty(label,name,value) \ { \ PROPNAME::GenerateValueAsString(); \ + m_delimiter = DELIMCHAR; \ } \ PROPNAME::~PROPNAME() { } \ -void PROPNAME::GenerateValueAsString() \ -{ \ - wxChar delimChar = DELIMCHAR; \ - if ( delimChar == wxS('"') ) \ - wxArrayStringProperty::GenerateValueAsString(); \ - else \ - wxPropertyGrid::ArrayStringToString(m_display, \ - m_value.GetArrayString(), \ - 0,DELIMCHAR,0); \ -} \ -bool PROPNAME::StringToValue( wxVariant& variant, \ - const wxString& text, int ) const \ -{ \ - wxChar delimChar = DELIMCHAR; \ - if ( delimChar == wxS('"') ) \ - return wxArrayStringProperty::StringToValue(variant, text, 0); \ - \ - wxArrayString arr; \ - WX_PG_TOKENIZER1_BEGIN(text,DELIMCHAR) \ - arr.Add( token ); \ - WX_PG_TOKENIZER1_END() \ - variant = arr; \ - return true; \ -} \ bool PROPNAME::OnEvent( wxPropertyGrid* propgrid, \ wxWindow* primary, wxEvent& event ) \ { \ diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index 8301bdf30f..88f580d83f 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -130,6 +130,15 @@ */ #define wxPG_DIR_DIALOG_MESSAGE wxS("DialogMessage") +/** + wxArrayStringProperty's string delimiter character. If this is aquotation + mark or hyphen, then strings will be quoted instead (with given + character). + + Default delimiter is quotation mark. +*/ +#define wxPG_ARRAY_DELIMITER wxS("Delimiter") + /** Sets displayed date format for wxDateProperty. */ #define wxPG_DATE_FORMAT wxS("DateFormat") diff --git a/samples/propgrid/sampleprops.cpp b/samples/propgrid/sampleprops.cpp index 00f9e7919e..5234207971 100644 --- a/samples/propgrid/sampleprops.cpp +++ b/samples/propgrid/sampleprops.cpp @@ -277,7 +277,8 @@ wxVariant wxPointProperty::ChildChanged( wxVariant& thisValue, // Dirs Property // ----------------------------------------------------------------------- -WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty,wxT(','),wxT("Browse")) +WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty, ',', + "Browse") #if wxUSE_VALIDATORS diff --git a/src/propgrid/props.cpp b/src/propgrid/props.cpp index b21beda0d4..9eea13d63b 100644 --- a/src/propgrid/props.cpp +++ b/src/propgrid/props.cpp @@ -2397,8 +2397,28 @@ void wxArrayStringProperty::OnSetValue() GenerateValueAsString(); } -#define ARRSTRPROP_ARRAY_TO_STRING(STRING,ARRAY) \ - wxPropertyGrid::ArrayStringToString(STRING,ARRAY,wxS('"'),wxS('"'),1) +void +wxArrayStringProperty::ConvertArrayToString(const wxArrayString& arr, + wxString* pString, + const wxUniChar& delimiter) const +{ + if ( delimiter == '"' || delimiter == '\'' ) + { + // Quoted strings + ArrayStringToString(*pString, + arr, + delimiter, + Escape | QuoteStrings); + } + else + { + // Regular delimiter + ArrayStringToString(*pString, + arr, + delimiter, + 0); + } +} wxString wxArrayStringProperty::ValueToString( wxVariant& WXUNUSED(value), int argFlags ) const @@ -2412,48 +2432,46 @@ wxString wxArrayStringProperty::ValueToString( wxVariant& WXUNUSED(value), wxArrayString arr = m_value.GetArrayString(); wxString s; - ARRSTRPROP_ARRAY_TO_STRING(s, arr); + ConvertArrayToString(arr, &s, m_delimiter); return s; } // Converts wxArrayString to a string separated by delimeters and spaces. // preDelim is useful for "str1" "str2" style. Set flags to 1 to do slash // conversion. -void wxPropertyGrid::ArrayStringToString( wxString& dst, const wxArrayString& src, - wxChar preDelim, wxChar postDelim, - int flags ) +void +wxArrayStringProperty::ArrayStringToString( wxString& dst, + const wxArrayString& src, + wxUniChar delimiter, int flags ) { wxString pdr; + wxString preas; unsigned int i; unsigned int itemCount = src.size(); - wxChar preas[2] = { 0, 0 }; - dst.Empty(); - if ( flags & 1 ) + if ( flags & Escape ) { - preas[0] = preDelim; + preas[0] = delimiter; pdr = wxS("\\"); - pdr += preDelim; + pdr += delimiter; } if ( itemCount ) dst.append( preas ); - wxASSERT( postDelim ); - wxString postDelimStr(postDelim); - //wxString preDelimStr(preDelim); + wxString delimStr(delimiter); for ( i = 0; i < itemCount; i++ ) { wxString str( src.Item(i) ); // Do some character conversion. - // Convertes \ to \\ and to \ - // Useful when preDelim and postDelim are "\"". - if ( flags & 1 ) + // Converts \ to \\ and $delimiter to \$delimiter + // Useful when quoting. + if ( flags & Escape ) { str.Replace( wxS("\\"), wxS("\\\\"), true ); if ( pdr.length() ) @@ -2464,19 +2482,19 @@ void wxPropertyGrid::ArrayStringToString( wxString& dst, const wxArrayString& sr if ( i < (itemCount-1) ) { - dst.append( postDelimStr ); + dst.append( delimStr ); dst.append( wxS(" ") ); dst.append( preas ); } - else if ( preDelim ) - dst.append( postDelimStr ); + else if ( flags & QuoteStrings ) + dst.append( delimStr ); } } void wxArrayStringProperty::GenerateValueAsString() { wxArrayString arr = m_value.GetArrayString(); - ARRSTRPROP_ARRAY_TO_STRING(m_display, arr); + ConvertArrayToString(arr, &m_display, m_delimiter); } // Default implementation doesn't do anything. @@ -2534,9 +2552,10 @@ bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid* propGrid, { wxArrayString actualValue = value.GetArrayString(); wxString tempStr; - ARRSTRPROP_ARRAY_TO_STRING(tempStr, actualValue); + ConvertArrayToString(actualValue, &tempStr, m_delimiter); #if wxUSE_VALIDATORS - if ( dialogValidator.DoValidate( propGrid, validator, tempStr ) ) + if ( dialogValidator.DoValidate(propGrid, validator, + tempStr) ) #endif { SetValueInEvent( actualValue ); @@ -2565,25 +2584,48 @@ bool wxArrayStringProperty::OnEvent( wxPropertyGrid* propGrid, return false; } -bool wxArrayStringProperty::StringToValue( wxVariant& variant, const wxString& text, int ) const +bool wxArrayStringProperty::StringToValue( wxVariant& variant, + const wxString& text, int ) const { wxArrayString arr; - WX_PG_TOKENIZER2_BEGIN(text,wxS('"')) + if ( m_delimiter == '"' || m_delimiter == '\'' ) + { + // Quoted strings + WX_PG_TOKENIZER2_BEGIN(text, m_delimiter) - // Need to replace backslashes with empty characters - // (opposite what is done in GenerateValueString). - token.Replace ( wxS("\\\\"), wxS("\\"), true ); + // Need to replace backslashes with empty characters + // (opposite what is done in ConvertArrayToString()). + token.Replace ( wxS("\\\\"), wxS("\\"), true ); - arr.Add( token ); + arr.Add( token ); - WX_PG_TOKENIZER2_END() + WX_PG_TOKENIZER2_END() + } + else + { + // Regular delimiter + WX_PG_TOKENIZER1_BEGIN(text, m_delimiter) + arr.Add( token ); + WX_PG_TOKENIZER1_END() + } variant = arr; return true; } +bool wxArrayStringProperty::DoSetAttribute( const wxString& name, wxVariant& value ) +{ + if ( name == wxPG_ARRAY_DELIMITER ) + { + m_delimiter = value.GetChar(); + GenerateValueAsString(); + return false; + } + return true; +} + // ----------------------------------------------------------------------- // wxPGInDialogValidator // ----------------------------------------------------------------------- -- 2.45.2