From 378f62bd08a2dd496ff8c1a273eba4132443210b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Sep 2018 16:52:20 +0200 Subject: [PATCH 1/5] Improve documentation of wxPGChoices ctor and Add() methods Mention that the "labels" array must be NULL-terminated and that "values" must have at least as many items, if they're provided at all. --- include/wx/propgrid/property.h | 4 ++-- interface/wx/propgrid/property.h | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index 46591191d6..a37a953fc5 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -719,7 +719,7 @@ public: } // Constructor. - // labels - Labels for choices. + // labels - Labels for choices, NULL-terminated. // values - Values for choices. If NULL, indexes are used. wxPGChoices( const wxChar* const* labels, const long* values = NULL ) { @@ -754,7 +754,7 @@ public: // Adds to current. // If did not have own copies, creates them now. If was empty, identical // to set except that creates copies. - // labels - Labels for added choices. + // labels - Labels for added choices, NULL-terminated. // values - Values for added choices. If empty, relevant entry indexes are used. void Add( const wxChar* const* labels, const ValArrItem* values = NULL ); diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index e12ad51e3f..483f220c20 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -2488,10 +2488,11 @@ public: Constructor. @param labels - Labels for choices. + Labels for choices, @NULL-terminated. @param values - Values for choices. If @NULL, indexes are used. + Values for choices. If @NULL, indexes are used. Otherwise must have + at least the same size as @a labels. */ wxPGChoices( const wxChar** labels, const long* values = NULL ); @@ -2502,7 +2503,8 @@ public: Labels for choices. @param values - Values for choices. If empty, indexes are used. + Values for choices. If empty, indexes are used. Otherwise must have + at least the same size as @a labels. */ wxPGChoices( const wxArrayString& labels, const wxArrayInt& values = wxArrayInt() ); @@ -2521,10 +2523,11 @@ public: identical to set except that creates copies. @param labels - Labels for added choices. + Labels for added choices, @NULL-terminated. @param values - Values for added choices. If empty, relevant entry indexes are used. + Values for added choices. If empty, relevant entry indexes are + used. Otherwise must have at least the same size as @a labels. */ void Add( const wxChar** labels, const ValArrItem* values = NULL ); From 8bb5cb1d6e59e4a0447d8d3f5d30f06c82275cef Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Sep 2018 16:57:10 +0200 Subject: [PATCH 2/5] Improve wxPGChoices::Set() documentation too Refer to Add() and use @overload rather than duplicating the description. --- interface/wx/propgrid/property.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index 483f220c20..f106f7cab4 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -2662,12 +2662,14 @@ public: /** Sets contents from lists of strings and values. + + This is similar to calling Clear() and the corresponding overload of Add(). */ void Set( const wxChar** labels, const long* values = NULL ); /** - Sets contents from lists of strings and values. - */ + @overload + */ void Set( const wxArrayString& labels, const wxArrayInt& values = wxArrayInt() ); /** From d2e072db3ce6e4be78a1e817e8be6d0cf9dd518f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Sep 2018 16:59:32 +0200 Subject: [PATCH 3/5] Don't use unclear ValArrItem typedef in wxPGChoices documentation All the other methods use just "long", so use it in Add() documentation as well instead of the rather cryptic ValArrItem. --- interface/wx/propgrid/property.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index f106f7cab4..758f9b810a 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -2529,7 +2529,7 @@ public: Values for added choices. If empty, relevant entry indexes are used. Otherwise must have at least the same size as @a labels. */ - void Add( const wxChar** labels, const ValArrItem* values = NULL ); + void Add( const wxChar** labels, const long* values = NULL ); /** Adds to current. Version that works with wxArrayString and wxArrayInt. From b624064659c37f995125e959c2b749f67c2c6b9d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Sep 2018 17:01:17 +0200 Subject: [PATCH 4/5] Call Add(), not Set(), from wxPGChoices ctors There is no need to call Free() on a newly constructed object and Set() is just Free() followed by Add(). No real changes. --- include/wx/propgrid/property.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index a37a953fc5..937fabcd06 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -724,7 +724,7 @@ public: wxPGChoices( const wxChar* const* labels, const long* values = NULL ) { Init(); - Set(labels,values); + Add(labels,values); } // Constructor. @@ -734,7 +734,7 @@ public: const wxArrayInt& values = wxArrayInt() ) { Init(); - Set(labels,values); + Add(labels,values); } // Simple interface constructor. From 96ecfd8c77a5dbe776ee6c4795a1f271e28ff00a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 Sep 2018 17:13:00 +0200 Subject: [PATCH 5/5] Provide wxPGChoices ctor, Add(), Set() overloads taking wxStrings Allow passing an array of wxStrings in addition to an array of wxChar* strings. --- include/wx/propgrid/property.h | 20 +++++++++++++++ interface/wx/propgrid/property.h | 43 +++++++++++++++++++++++++++++++- src/propgrid/property.cpp | 14 +++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index 937fabcd06..82b5c3bb83 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -719,6 +719,17 @@ public: } // Constructor. + // count - Number of labels. + // labels - Labels themselves. + // values - Values for choices. If NULL, indexes are used. + wxPGChoices(size_t count, const wxString* labels, const long* values = NULL) + { + Init(); + Add(count, labels, values); + } + + // Constructor overload taking wxChar strings, provided mostly for + // compatibility. // labels - Labels for choices, NULL-terminated. // values - Values for choices. If NULL, indexes are used. wxPGChoices( const wxChar* const* labels, const long* values = NULL ) @@ -754,6 +765,9 @@ public: // Adds to current. // If did not have own copies, creates them now. If was empty, identical // to set except that creates copies. + void Add(size_t count, const wxString* labels, const long* values = NULL); + + // Overload taking wxChar strings, provided mostly for compatibility. // labels - Labels for added choices, NULL-terminated. // values - Values for added choices. If empty, relevant entry indexes are used. void Add( const wxChar* const* labels, const ValArrItem* values = NULL ); @@ -879,6 +893,12 @@ public: // Sets contents from lists of strings and values. // Does not create copies for itself. // TODO: Deprecate. + void Set(size_t count, const wxString* labels, const long* values = NULL) + { + Free(); + Add(count, labels, values); + } + void Set( const wxChar* const* labels, const long* values = NULL ) { Free(); diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index 758f9b810a..e60002a638 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -2487,6 +2487,24 @@ public: /** Constructor. + @param count + Number of the strings in @a labels array. + @param labels + Labels for choices. + @param values + Values for choices. If @NULL, indexes are used. Otherwise must have + at least @a count elements. + + @since 3.1.2 + */ + wxPGChoices(size_t count, const wxString* labels, const long* values = NULL); + + /** + Constructor overload taking wxChar strings. + + This constructor is provided mostly for compatibility, prefer to use + one of the other constructor overloads in the new code. + @param labels Labels for choices, @NULL-terminated. @@ -2522,6 +2540,24 @@ public: Adds to current. If did not have own copies, creates them now. If was empty, identical to set except that creates copies. + @param count + Number of the strings in @a labels array. + @param labels + Labels for choices. + @param values + Values for choices. If @NULL, indexes are used. Otherwise must have + at least @a count elements. + + @since 3.1.2 + */ + void Add(size_t count, const wxString* labels, const long* values = NULL); + + /** + Adds to current. + + This overload is provided mostly for compatibility, prefer to use one + of the other ones in the new code. + @param labels Labels for added choices, @NULL-terminated. @@ -2532,7 +2568,7 @@ public: void Add( const wxChar** labels, const long* values = NULL ); /** - Adds to current. Version that works with wxArrayString and wxArrayInt. + @overload */ void Add( const wxArrayString& arr, const wxArrayInt& arrint ); @@ -2665,6 +2701,11 @@ public: This is similar to calling Clear() and the corresponding overload of Add(). */ + void Set(size_t count, const wxString* labels, const long* values = NULL); + + /** + @overload + */ void Set( const wxChar** labels, const long* values = NULL ); /** diff --git a/src/propgrid/property.cpp b/src/propgrid/property.cpp index 281ed15d23..5cfa571546 100644 --- a/src/propgrid/property.cpp +++ b/src/propgrid/property.cpp @@ -3013,6 +3013,20 @@ wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value ) // ----------------------------------------------------------------------- +void wxPGChoices::Add(size_t count, const wxString* labels, const long* values) +{ + AllocExclusive(); + + for ( size_t i = 0; i < count; ++i ) + { + const int value = values ? values[i] : i; + wxPGChoiceEntry entry(labels[i], value); + m_data->Insert( i, entry ); + } +} + +// ----------------------------------------------------------------------- + void wxPGChoices::Add( const wxChar* const* labels, const ValArrItem* values ) { AllocExclusive();