From d61d8cffa928c7b25cfada5618b987be2ec5e052 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Tue, 7 Oct 2008 17:28:05 +0000 Subject: [PATCH] Eliminated last property generation macros from props.h (colour property gen ones, now use subclassing facilities present in wx(System)ColourProperty) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56150 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/propgrid/advprops.h | 23 ++++- include/wx/propgrid/props.h | 139 +------------------------------ interface/wx/propgrid/property.h | 27 +++++- samples/propgrid/tests.cpp | 47 ++--------- src/propgrid/advprops.cpp | 66 +++++++++++++-- 5 files changed, 116 insertions(+), 186 deletions(-) diff --git a/include/wx/propgrid/advprops.h b/include/wx/propgrid/advprops.h index 25cd2586d8..042d6021ab 100644 --- a/include/wx/propgrid/advprops.h +++ b/include/wx/propgrid/advprops.h @@ -282,14 +282,29 @@ protected: // ----------------------------------------------------------------------- -WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR_WITH_DECL( - wxColourProperty, class WXDLLIMPEXP_PROPGRID) +class WXDLLIMPEXP_PROPGRID wxColourProperty : public wxSystemColourProperty +{ + WX_PG_DECLARE_PROPERTY_CLASS(wxColourProperty) +public: + wxColourProperty( const wxString& label = wxPG_LABEL, + const wxString& name = wxPG_LABEL, + const wxColour& value = *wxWHITE ); + virtual ~wxColourProperty(); -// Exclude classes from wxPython bindings -#ifndef SWIG +protected: + virtual wxString GetValueAsString( int argFlags ) const; + virtual wxColour GetColour( int index ) const; + virtual wxVariant DoTranslateVal( wxColourPropertyValue& v ) const; + +private: + void Init( wxColour colour ); +}; // ----------------------------------------------------------------------- +// Exclude classes from wxPython bindings +#ifndef SWIG + /** @class wxCursorProperty @ingroup classes Property representing wxCursor. diff --git a/include/wx/propgrid/props.h b/include/wx/propgrid/props.h index edda643d04..6609bdbb3a 100644 --- a/include/wx/propgrid/props.h +++ b/include/wx/propgrid/props.h @@ -26,144 +26,9 @@ class wxArrayEditorDialog; // Property class implementation helper macros. // -// Adds constructor function as well. -#define WX_PG_IMPLEMENT_PROPERTY_CLASS2(NAME,CLASSNAME,\ - UPCLASS,T,T_AS_ARG,EDITOR) \ +#define WX_PG_IMPLEMENT_PROPERTY_CLASS(NAME, UPCLASS, T, T_AS_ARG, EDITOR) \ IMPLEMENT_DYNAMIC_CLASS(NAME, UPCLASS) \ -WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(NAME,T,EDITOR) - -// A regular property -#define WX_PG_IMPLEMENT_PROPERTY_CLASS(NAME,UPNAME,T,T_AS_ARG,EDITOR) \ -WX_PG_IMPLEMENT_PROPERTY_CLASS2(NAME,NAME,UPNAME,T,T_AS_ARG,EDITOR) - -// ----------------------------------------------------------------------- - -#define WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_WITH_DECL(CLASSNAME, DECL) \ -DECL CLASSNAME : public wxSystemColourProperty \ -{ \ - DECLARE_DYNAMIC_CLASS(CLASSNAME) \ -public: \ - CLASSNAME( const wxString& label = wxPG_LABEL, \ - const wxString& name = wxPG_LABEL, \ - const wxColourPropertyValue& value = wxColourPropertyValue() ); \ - virtual ~CLASSNAME(); \ - virtual wxColour GetColour( int index ) const; \ -}; - -#define WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY(CLASSNAME) \ -WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_WITH_DECL(CLASSNAME, class) - -#define WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY(CLASSNAME,\ - LABELS,VALUES,COLOURS) \ -static wxPGChoices gs_##CLASSNAME##_choicesCache; \ -IMPLEMENT_DYNAMIC_CLASS(CLASSNAME, wxSystemColourProperty) \ -CLASSNAME::CLASSNAME( const wxString& label, const wxString& name, \ - const wxColourPropertyValue& value ) \ - : wxSystemColourProperty(label, name, LABELS, VALUES, \ - &gs_##CLASSNAME##_choicesCache, value ) \ -{ \ - m_flags |= wxPG_PROP_TRANSLATE_CUSTOM; \ -} \ -CLASSNAME::~CLASSNAME () { } \ -wxColour CLASSNAME::GetColour ( int index ) const \ -{ \ - if ( !m_choices.HasValue(index) ) \ - { \ - wxASSERT( index < (int)m_choices.GetCount() ); \ - return COLOURS[index]; \ - } \ - return COLOURS[m_choices.GetValue(index)]; \ -} - -// ----------------------------------------------------------------------- - -#define WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR_WITH_DECL(CLASSNAME,\ - DECL) \ -DECL CLASSNAME : public wxSystemColourProperty \ -{ \ - WX_PG_DECLARE_PROPERTY_CLASS(CLASSNAME) \ -public: \ - CLASSNAME( const wxString& label = wxPG_LABEL, \ - const wxString& name = wxPG_LABEL, \ - const wxColour& value = wxColour() ); \ - virtual ~CLASSNAME(); \ - virtual wxString GetValueAsString( int argFlags ) const; \ - virtual wxColour GetColour( int index ) const; \ - virtual wxVariant DoTranslateVal( wxColourPropertyValue& v ) const; \ - void Init( wxColour colour ); \ -}; - -#define WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR(CLASSNAME) \ -WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR_WITH_DECL(CLASSNAME, class) - -#define WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR2(CLASSNAME, \ - LABELS, \ - VALUES, \ - COLOURS, \ - EDITOR) \ -static wxPGChoices gs_##CLASSNAME##_choicesCache; \ -WX_PG_IMPLEMENT_PROPERTY_CLASS(CLASSNAME, wxSystemColourProperty, \ - wxColour, const wxColour&,EDITOR) \ -CLASSNAME::CLASSNAME( const wxString& label, \ - const wxString& name, \ - const wxColour& value ) \ - : wxSystemColourProperty(label, name, LABELS, VALUES, \ - &gs_##CLASSNAME##_choicesCache, value ) \ -{ \ - if ( &value ) \ - Init( value ); \ - else \ - Init( *wxWHITE ); \ - m_flags |= wxPG_PROP_TRANSLATE_CUSTOM; \ -} \ -CLASSNAME::~CLASSNAME() { } \ -void CLASSNAME::Init( wxColour colour ) \ -{ \ - if ( !colour.Ok() ) \ - colour = *wxWHITE; \ - wxVariant variant; \ - variant << colour; \ - m_value = variant; \ - int ind = ColToInd(colour); \ - if ( ind < 0 ) \ - ind = m_choices.GetCount() - 1; \ - SetIndex( ind ); \ -} \ -wxString CLASSNAME::GetValueAsString( int argFlags ) const \ -{ \ - const wxPGEditor* editor = GetEditorClass(); \ - if ( editor != wxPGEditor_Choice && \ - editor != wxPGEditor_ChoiceAndButton && \ - editor != wxPGEditor_ComboBox ) \ - argFlags |= wxPG_PROPERTY_SPECIFIC; \ - return wxSystemColourProperty::GetValueAsString(argFlags); \ -} \ -wxColour CLASSNAME::GetColour( int index ) const \ -{ \ - if ( !m_choices.HasValue(index) ) \ - { \ - wxASSERT( index < (int)GetItemCount() ); \ - return COLOURS[index]; \ - } \ - return COLOURS[m_choices.GetValue(index)]; \ -} \ -wxVariant CLASSNAME::DoTranslateVal( wxColourPropertyValue& v ) const \ -{ \ - wxVariant variant; \ - variant << v.m_colour; \ - return variant; \ -} - - -#define WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR(CLASSNAME, \ - LABELS, \ - VALUES, \ - COLOURS) \ -WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR2(CLASSNAME, \ - LABELS, \ - VALUES, \ - COLOURS, \ - Choice) +WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(NAME, T, EDITOR) // ----------------------------------------------------------------------- diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index 6795ed0a18..229a8ede3c 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -329,6 +329,8 @@ Useful alternate editor: Choice. Represents wxColour. wxButton is used to trigger a colour picker dialog. + There are various sub-classing opportunities with this class. See + below in wxSystemColourProperty section for details. @subsection wxFontProperty @@ -339,7 +341,9 @@ Represents wxColour and a system colour index. wxChoice is used to edit the value. Drop-down list has color images. Note that value type - is wxColourPropertyValue instead of wxColour. + is wxColourPropertyValue instead of wxColour (which wxColourProperty + uses). + @code class wxColourPropertyValue : public wxObject { @@ -356,6 +360,27 @@ wxColour m_colour; }; @endcode + + in wxSystemColourProperty, and its derived class wxColourProperty, there + are various sub-classing features. To set basic list list of colour + names, call wxPGProperty::SetChoices(). + + @code + // Override in derived class to customize how colours are translated + // to strings. + virtual wxString ColourToString( const wxColour& col, int index ) const; + + // Returns index of entry that triggers colour picker dialog + // (default is last). + virtual int GetCustomColourIndex() const; + + // Helper function to show the colour dialog + bool QueryColourFromUser( wxVariant& variant ) const; + + // Returns colour for given choice. + // Default function returns wxSystemSettings::GetColour(index). + virtual wxColour GetColour( int index ) const; + @endcode @subsection wxCursorProperty diff --git a/samples/propgrid/tests.cpp b/samples/propgrid/tests.cpp index a237687684..bbecd179b0 100644 --- a/samples/propgrid/tests.cpp +++ b/samples/propgrid/tests.cpp @@ -81,23 +81,6 @@ static unsigned long mycolprop_colours[] = { wxPG_COLOUR(0,0,0) }; -// Implement property class. Third argument is optional values array, -// but in this example we are only interested in creating a shortcut -// for user to access the colour values. Last arg is itemcount, but -// it will be deprecated in the future. -WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR(wxMyColourProperty) -WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR(wxMyColourProperty, - mycolprop_labels, - (long*)NULL, - mycolprop_colours) - - -WX_PG_DECLARE_CUSTOM_COLOUR_PROPERTY(wxMyColour2Property) -WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY(wxMyColour2Property, - mycolprop_labels, - (long*)NULL, - mycolprop_colours) - // ----------------------------------------------------------------------- // @@ -106,10 +89,10 @@ WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY(wxMyColour2Property, // * Includes custom colour entry. // * Includes extra custom entry. // -class MyColourProperty3 : public wxColourProperty +class MyColourProperty : public wxColourProperty { public: - MyColourProperty3( const wxString& label = wxPG_LABEL, + MyColourProperty( const wxString& label = wxPG_LABEL, const wxString& name = wxPG_LABEL, const wxColour& value = *wxWHITE ) : wxColourProperty(label, name, value) @@ -129,7 +112,7 @@ public: SetValue(variant); } - virtual ~MyColourProperty3() + virtual ~MyColourProperty() { } @@ -174,26 +157,12 @@ public: void FormMain::AddTestProperties( wxPropertyGridPage* pg ) { - pg->Append( new wxMyColourProperty(wxT("CustomColourProperty1")) ); - - pg->SetPropertyHelpString(wxT("CustomColourProperty1"), - wxT("This is a wxMyColourProperty from the sample app. ") - wxT("It is built with WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR macro ") - wxT("and has wxColour as its data type")); - - pg->Append( new wxMyColour2Property(wxT("CustomColourProperty2")) ); - - pg->SetPropertyHelpString(wxT("CustomColourProperty2"), - wxT("This is a wxMyColour2Property from the sample app. ") - wxT("It is built with WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY macro ") - wxT("and has wxColourPropertyValue as its data type")); - - pg->Append( new MyColourProperty3(wxT("CustomColourProperty3"), wxPG_LABEL, *wxGREEN) ); - pg->GetProperty(wxT("CustomColourProperty3"))->SetFlag(wxPG_PROP_AUTO_UNSPECIFIED); - pg->SetPropertyEditor( wxT("CustomColourProperty3"), wxPGEditor_ComboBox ); + pg->Append( new MyColourProperty(wxT("CustomColourProperty"), wxPG_LABEL, *wxGREEN) ); + pg->GetProperty(wxT("CustomColourProperty"))->SetFlag(wxPG_PROP_AUTO_UNSPECIFIED); + pg->SetPropertyEditor( wxT("CustomColourProperty"), wxPGEditor_ComboBox ); - pg->SetPropertyHelpString(wxT("CustomColourProperty3"), - wxT("This is a MyColourProperty3 from the sample app. ") + pg->SetPropertyHelpString(wxT("CustomColourProperty"), + wxT("This is a MyColourProperty from the sample app. ") wxT("It is built by subclassing wxColourProperty.")); } diff --git a/src/propgrid/advprops.cpp b/src/propgrid/advprops.cpp index cfa421adf4..1f556ddbcf 100644 --- a/src/propgrid/advprops.cpp +++ b/src/propgrid/advprops.cpp @@ -1356,11 +1356,67 @@ static unsigned long gs_cp_es_normcolour_colours[] = { wxPG_COLOUR(0,0,0) }; -WX_PG_IMPLEMENT_CUSTOM_COLOUR_PROPERTY_USES_WXCOLOUR2(wxColourProperty, - gs_cp_es_normcolour_labels, - (const long*)NULL, - gs_cp_es_normcolour_colours, - TextCtrlAndButton) +WX_PG_IMPLEMENT_PROPERTY_CLASS(wxColourProperty, wxSystemColourProperty, + wxColour, const wxColour&, TextCtrlAndButton) + +static wxPGChoices gs_wxColourProperty_choicesCache; + +wxColourProperty::wxColourProperty( const wxString& label, + const wxString& name, + const wxColour& value ) + : wxSystemColourProperty(label, name, gs_cp_es_normcolour_labels, + NULL, + &gs_wxColourProperty_choicesCache, value ) +{ + Init( value ); + + m_flags |= wxPG_PROP_TRANSLATE_CUSTOM; +} + +wxColourProperty::~wxColourProperty() +{ +} + +void wxColourProperty::Init( wxColour colour ) +{ + if ( !colour.Ok() ) + colour = *wxWHITE; + wxVariant variant; + variant << colour; + m_value = variant; + int ind = ColToInd(colour); + if ( ind < 0 ) + ind = m_choices.GetCount() - 1; + SetIndex( ind ); +} + +wxString wxColourProperty::GetValueAsString( int argFlags ) const +{ + const wxPGEditor* editor = GetEditorClass(); + if ( editor != wxPGEditor_Choice && + editor != wxPGEditor_ChoiceAndButton && + editor != wxPGEditor_ComboBox ) + argFlags |= wxPG_PROPERTY_SPECIFIC; + + return wxSystemColourProperty::GetValueAsString(argFlags); +} + +wxColour wxColourProperty::GetColour( int index ) const +{ + if ( !m_choices.HasValue(index) ) + { + wxASSERT( index < (int)GetItemCount() ); + return gs_cp_es_normcolour_colours[index]; + } + return gs_cp_es_normcolour_colours[m_choices.GetValue(index)]; +} + +wxVariant wxColourProperty::DoTranslateVal( wxColourPropertyValue& v ) const +{ + wxVariant variant; + variant << v.m_colour; + return variant; +} // ----------------------------------------------------------------------- // wxCursorProperty -- 2.45.2