Adapted wxPropertyGrid documentation, samples, tests, and wxVariantData-macros to the new wxAny<->wxVariant conversions
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64001 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -444,30 +444,52 @@ To use them, you have to include <wx/propgrid/advprops.h>.
|
|||||||
|
|
||||||
@section propgrid_processingvalues Processing Property Values
|
@section propgrid_processingvalues Processing Property Values
|
||||||
|
|
||||||
Properties store their values internally in wxVariant. You can obtain
|
Properties store their values internally as wxVariant, but is also possible to
|
||||||
this value using wxPGProperty::GetValue() or wxPropertyGridInterface::
|
obtain them as wxAny, using implicit conversion. You can get property
|
||||||
GetPropertyValue().
|
values with wxPGProperty::GetValue() and
|
||||||
|
wxPropertyGridInterface::GetPropertyValue().
|
||||||
|
|
||||||
If you wish to obtain property value in specific data type, you can
|
Below is a code example which handles wxEVT_PG_CHANGED event:
|
||||||
call various getter functions, such as wxPropertyGridInterface::
|
|
||||||
GetPropertyValueAsString(), which, as name might say, returns property
|
|
||||||
value's string representation. While this particular function is very
|
|
||||||
safe to use for any kind of property, some might display error message
|
|
||||||
if property value is not in compatible enough format. For instance,
|
|
||||||
wxPropertyGridInterface::GetPropertyValueAsLongLong() will support
|
|
||||||
long as well as wxLongLong, but GetPropertyValueAsArrayString() only
|
|
||||||
supports wxArrayString and nothing else.
|
|
||||||
|
|
||||||
In any case, you will need to take extra care when dealing with
|
@code
|
||||||
raw wxVariant values. For instance, wxIntProperty and wxUIntProperty,
|
|
||||||
store value internally as wx(U)LongLong when number doesn't fit into
|
|
||||||
standard long type. Using << operator to get wx(U)LongLong from wxVariant
|
|
||||||
is customized to work quite safely with various types of variant data.
|
|
||||||
|
|
||||||
You may have noticed that properties store, in wxVariant, values of many
|
void MyWindowClass::OnPropertyGridChanged(wxPropertyGridEvent& event)
|
||||||
types which are not natively supported by it. Custom wxVariantDatas
|
{
|
||||||
are therefore implemented and << and >> operators implemented to
|
wxPGProperty* property = event.GetProperty();
|
||||||
convert data from and to wxVariant.
|
|
||||||
|
// Do nothing if event did not have associated property
|
||||||
|
if ( !property )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// GetValue() returns wxVariant, but it is converted transparently to
|
||||||
|
// wxAny
|
||||||
|
wxAny value = property->GetValue();
|
||||||
|
|
||||||
|
// Also, handle the case where property value is unspecified
|
||||||
|
if ( value.IsNull() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Handle changes in values, as needed
|
||||||
|
if ( property.GetName() == "MyStringProperty" )
|
||||||
|
OnMyStringPropertyChanged(value.As<wxString>());
|
||||||
|
else if ( property.GetName() == "MyColourProperty" )
|
||||||
|
OnMyColourPropertyChanged(value.As<wxColour>());
|
||||||
|
}
|
||||||
|
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
You can get a string-representation of property's value using
|
||||||
|
wxPGProperty::GetValueAsString() or
|
||||||
|
wxPropertyGridInterface::GetPropertyValueAsString(). This particular function
|
||||||
|
is very safe to use with any kind of property.
|
||||||
|
|
||||||
|
@note There is a one case in which you may want to take extra care when
|
||||||
|
dealing with raw wxVariant values. That is, integer-type properties,
|
||||||
|
such as wxIntProperty and wxUIntProperty, store value internally as
|
||||||
|
wx(U)LongLong when number doesn't fit into standard long type. Using
|
||||||
|
<< operator to get wx(U)LongLong from wxVariant is customized to work
|
||||||
|
quite safely with various types of variant data. However, you can also
|
||||||
|
bypass this problem by using wxAny in your code instead of wxVariant.
|
||||||
|
|
||||||
Note that in some cases property value can be Null variant, which means
|
Note that in some cases property value can be Null variant, which means
|
||||||
that property value is unspecified. This usually occurs only when
|
that property value is unspecified. This usually occurs only when
|
||||||
|
@@ -18,6 +18,7 @@
|
|||||||
#include "wx/vector.h"
|
#include "wx/vector.h"
|
||||||
#include "wx/hashmap.h"
|
#include "wx/hashmap.h"
|
||||||
#include "wx/variant.h"
|
#include "wx/variant.h"
|
||||||
|
#include "wx/any.h"
|
||||||
#include "wx/longlong.h"
|
#include "wx/longlong.h"
|
||||||
#include "wx/clntdata.h"
|
#include "wx/clntdata.h"
|
||||||
|
|
||||||
@@ -545,10 +546,13 @@ public:\
|
|||||||
\
|
\
|
||||||
virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
|
virtual wxVariantData* Clone() const { return new classname##VariantData(m_value); } \
|
||||||
\
|
\
|
||||||
|
DECLARE_WXANY_CONVERSION() \
|
||||||
protected:\
|
protected:\
|
||||||
classname m_value; \
|
classname m_value; \
|
||||||
};\
|
};\
|
||||||
\
|
\
|
||||||
|
IMPLEMENT_TRIVIAL_WXANY_CONVERSION(classname, classname##VariantData) \
|
||||||
|
\
|
||||||
wxString classname##VariantData::GetType() const\
|
wxString classname##VariantData::GetType() const\
|
||||||
{\
|
{\
|
||||||
return wxS(#classname);\
|
return wxS(#classname);\
|
||||||
|
@@ -926,25 +926,36 @@ void FormMain::OnPropertyGridChange( wxPropertyGridEvent& event )
|
|||||||
wxPGProperty* property = event.GetProperty();
|
wxPGProperty* property = event.GetProperty();
|
||||||
|
|
||||||
const wxString& name = property->GetName();
|
const wxString& name = property->GetName();
|
||||||
wxVariant value = property->GetValue();
|
|
||||||
|
// Properties store values internally as wxVariants, but it is preferred
|
||||||
|
// to use the more modern wxAny at the interface level
|
||||||
|
wxAny value = property->GetValue();
|
||||||
|
|
||||||
// Don't handle 'unspecified' values
|
// Don't handle 'unspecified' values
|
||||||
if ( value.IsNull() )
|
if ( value.IsNull() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
//
|
||||||
|
// FIXME-VC6: In order to compile on Visual C++ 6.0, wxANY_AS()
|
||||||
|
// macro is used. Unless you want to support this old
|
||||||
|
// compiler in your own code, you can use the more
|
||||||
|
// nicer form value.As<FOO>() instead of
|
||||||
|
// wxANY_AS(value, FOO).
|
||||||
|
//
|
||||||
|
|
||||||
// Some settings are disabled outside Windows platform
|
// Some settings are disabled outside Windows platform
|
||||||
if ( name == wxT("X") )
|
if ( name == wxT("X") )
|
||||||
SetSize ( m_pPropGridManager->GetPropertyValueAsInt(property), -1, -1, -1, wxSIZE_USE_EXISTING );
|
SetSize( wxANY_AS(value, int), -1, -1, -1, wxSIZE_USE_EXISTING );
|
||||||
else if ( name == wxT("Y") )
|
else if ( name == wxT("Y") )
|
||||||
// wxPGVariantToInt is safe long int value getter
|
// wxPGVariantToInt is safe long int value getter
|
||||||
SetSize ( -1, value.GetLong(), -1, -1, wxSIZE_USE_EXISTING );
|
SetSize ( -1, wxANY_AS(value, int), -1, -1, wxSIZE_USE_EXISTING );
|
||||||
else if ( name == wxT("Width") )
|
else if ( name == wxT("Width") )
|
||||||
SetSize ( -1, -1, m_pPropGridManager->GetPropertyValueAsInt(property), -1, wxSIZE_USE_EXISTING );
|
SetSize ( -1, -1, wxANY_AS(value, int), -1, wxSIZE_USE_EXISTING );
|
||||||
else if ( name == wxT("Height") )
|
else if ( name == wxT("Height") )
|
||||||
SetSize ( -1, -1, -1, value.GetLong(), wxSIZE_USE_EXISTING );
|
SetSize ( -1, -1, -1, wxANY_AS(value, int), wxSIZE_USE_EXISTING );
|
||||||
else if ( name == wxT("Label") )
|
else if ( name == wxT("Label") )
|
||||||
{
|
{
|
||||||
SetTitle ( m_pPropGridManager->GetPropertyValueAsString(property) );
|
SetTitle( wxANY_AS(value, wxString) );
|
||||||
}
|
}
|
||||||
else if ( name == wxT("Password") )
|
else if ( name == wxT("Password") )
|
||||||
{
|
{
|
||||||
@@ -958,8 +969,7 @@ void FormMain::OnPropertyGridChange( wxPropertyGridEvent& event )
|
|||||||
else
|
else
|
||||||
if ( name == wxT("Font") )
|
if ( name == wxT("Font") )
|
||||||
{
|
{
|
||||||
wxFont font;
|
wxFont font = wxANY_AS(value, wxFont);
|
||||||
font << value;
|
|
||||||
wxASSERT( font.Ok() );
|
wxASSERT( font.Ok() );
|
||||||
|
|
||||||
m_pPropGridManager->SetFont( font );
|
m_pPropGridManager->SetFont( font );
|
||||||
@@ -967,26 +977,22 @@ void FormMain::OnPropertyGridChange( wxPropertyGridEvent& event )
|
|||||||
else
|
else
|
||||||
if ( name == wxT("Margin Colour") )
|
if ( name == wxT("Margin Colour") )
|
||||||
{
|
{
|
||||||
wxColourPropertyValue cpv;
|
wxColourPropertyValue cpv = wxANY_AS(value, wxColourPropertyValue);
|
||||||
cpv << value;
|
|
||||||
m_pPropGridManager->GetGrid()->SetMarginColour( cpv.m_colour );
|
m_pPropGridManager->GetGrid()->SetMarginColour( cpv.m_colour );
|
||||||
}
|
}
|
||||||
else if ( name == wxT("Cell Colour") )
|
else if ( name == wxT("Cell Colour") )
|
||||||
{
|
{
|
||||||
wxColourPropertyValue cpv;
|
wxColourPropertyValue cpv = wxANY_AS(value, wxColourPropertyValue);
|
||||||
cpv << value;
|
|
||||||
m_pPropGridManager->GetGrid()->SetCellBackgroundColour( cpv.m_colour );
|
m_pPropGridManager->GetGrid()->SetCellBackgroundColour( cpv.m_colour );
|
||||||
}
|
}
|
||||||
else if ( name == wxT("Line Colour") )
|
else if ( name == wxT("Line Colour") )
|
||||||
{
|
{
|
||||||
wxColourPropertyValue cpv;
|
wxColourPropertyValue cpv = wxANY_AS(value, wxColourPropertyValue);
|
||||||
cpv << value;
|
|
||||||
m_pPropGridManager->GetGrid()->SetLineColour( cpv.m_colour );
|
m_pPropGridManager->GetGrid()->SetLineColour( cpv.m_colour );
|
||||||
}
|
}
|
||||||
else if ( name == wxT("Cell Text Colour") )
|
else if ( name == wxT("Cell Text Colour") )
|
||||||
{
|
{
|
||||||
wxColourPropertyValue cpv;
|
wxColourPropertyValue cpv = wxANY_AS(value, wxColourPropertyValue);
|
||||||
cpv << value;
|
|
||||||
m_pPropGridManager->GetGrid()->SetCellTextColour( cpv.m_colour );
|
m_pPropGridManager->GetGrid()->SetCellTextColour( cpv.m_colour );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -501,6 +501,59 @@ bool FormMain::RunTests( bool fullTest, bool interactive )
|
|||||||
pgman = m_pPropGridManager;
|
pgman = m_pPropGridManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Test wxAny<->wxVariant conversion
|
||||||
|
RT_START_TEST(WXVARIANT_TO_WXANY_CONVERSION)
|
||||||
|
|
||||||
|
wxPGProperty* prop;
|
||||||
|
wxAny any;
|
||||||
|
|
||||||
|
#if wxUSE_DATETIME
|
||||||
|
prop = pgman->GetProperty("DateProperty");
|
||||||
|
wxDateTime testTime = wxDateTime::Now();
|
||||||
|
any = testTime;
|
||||||
|
prop->SetValue(any);
|
||||||
|
if ( prop->GetValue().GetAny().As<wxDateTime>() != testTime )
|
||||||
|
RT_FAILURE();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
prop = pgman->GetProperty("IntProperty");
|
||||||
|
int testInt = 25537983;
|
||||||
|
any = testInt;
|
||||||
|
prop->SetValue(any);
|
||||||
|
if ( prop->GetValue().GetAny().As<int>() != testInt )
|
||||||
|
RT_FAILURE();
|
||||||
|
#ifdef wxLongLong_t
|
||||||
|
if ( prop->GetValue().GetAny().As<wxLongLong_t>() != testInt )
|
||||||
|
RT_FAILURE();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
prop = pgman->GetProperty("StringProperty");
|
||||||
|
wxString testString = "asd934jfyn3";
|
||||||
|
any = testString;
|
||||||
|
prop->SetValue(any);
|
||||||
|
if ( prop->GetValue().GetAny().As<wxString>() != testString )
|
||||||
|
RT_FAILURE();
|
||||||
|
|
||||||
|
// Test with a type generated with IMPLEMENT_VARIANT_OBJECT()
|
||||||
|
prop = pgman->GetProperty("ColourProperty");
|
||||||
|
wxColour testCol = *wxCYAN;
|
||||||
|
any = testCol;
|
||||||
|
prop->SetValue(any);
|
||||||
|
if ( prop->GetValue().GetAny().As<wxColour>() != testCol )
|
||||||
|
RT_FAILURE();
|
||||||
|
|
||||||
|
// Test with a type with custom wxVariantData defined by
|
||||||
|
// wxPG headers.
|
||||||
|
prop = pgman->GetProperty("Position");
|
||||||
|
wxPoint testPoint(199, 199);
|
||||||
|
any = testPoint;
|
||||||
|
prop->SetValue(any);
|
||||||
|
if ( prop->GetValue().GetAny().As<wxPoint>() != testPoint )
|
||||||
|
RT_FAILURE();
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
RT_START_TEST(GetPropertyValues)
|
RT_START_TEST(GetPropertyValues)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user