From b6fd0b424573e09ae7e83a28b225bbba008157b7 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Mon, 12 Jan 2009 17:05:37 +0000 Subject: [PATCH] Better support for unspecified property value in wxDateProperty and DatePickerCtrl editor, especially when wxDP_ALLOWNONE is used git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58051 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/propgrid/advprops.h | 4 ++- interface/wx/propgrid/property.h | 4 ++- samples/propgrid/propgrid.cpp | 8 +++--- src/propgrid/advprops.cpp | 42 +++++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/include/wx/propgrid/advprops.h b/include/wx/propgrid/advprops.h index bb02c2eaea..1119081c86 100644 --- a/include/wx/propgrid/advprops.h +++ b/include/wx/propgrid/advprops.h @@ -430,7 +430,8 @@ protected: Supported special attributes: - "DateFormat": Determines displayed date format. - "PickerStyle": Determines window style used with wxDatePickerCtrl. - Default is wxDP_DEFAULT | wxDP_SHOWCENTURY. + Default is wxDP_DEFAULT | wxDP_SHOWCENTURY. Using wxDP_ALLOWNONE + enables additional support for unspecified property value. */ class WXDLLIMPEXP_PROPGRID wxDateProperty : public wxPGProperty { @@ -442,6 +443,7 @@ public: const wxDateTime& value = wxDateTime() ); virtual ~wxDateProperty(); + virtual void OnSetValue(); virtual wxString ValueToString( wxVariant& value, int argFlags = 0 ) const; virtual bool StringToValue(wxVariant& variant, const wxString& text, diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index c78af7a893..372e6d401e 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -115,7 +115,8 @@ #define wxPG_DATE_FORMAT wxS("DateFormat") /** Sets wxDatePickerCtrl window style used with wxDateProperty. Default - is wxDP_DEFAULT | wxDP_SHOWCENTURY. + is wxDP_DEFAULT | wxDP_SHOWCENTURY. Using wxDP_ALLOWNONE will enable + better unspecified value support in the editor. */ #define wxPG_DATE_PICKER_STYLE wxS("PickerStyle") @@ -310,6 +311,7 @@ string wxDateTime::Format uses (altough default is recommended as it is locale-dependant), and wxPG_DATE_PICKER_STYLE allows changing window style given to DatePickerCtrl (default is wxDP_DEFAULT|wxDP_SHOWCENTURY). + Using wxDP_ALLOWNONE will enable better unspecified value support. @subsection wxEditEnumProperty diff --git a/samples/propgrid/propgrid.cpp b/samples/propgrid/propgrid.cpp index 31c1483878..c413502b23 100644 --- a/samples/propgrid/propgrid.cpp +++ b/samples/propgrid/propgrid.cpp @@ -1593,11 +1593,13 @@ void FormMain::PopulateWithExamples () #if wxUSE_DATEPICKCTRL pg->SetPropertyAttribute( wxT("DateProperty"), wxPG_DATE_PICKER_STYLE, - (long)(wxDP_DROPDOWN | wxDP_SHOWCENTURY) ); + (long)(wxDP_DROPDOWN | + wxDP_SHOWCENTURY | + wxDP_ALLOWNONE) ); pg->SetPropertyHelpString( wxT("DateProperty"), - wxT("Attribute wxPG_DATE_PICKER_STYLE has been set to (long)(wxDP_DROPDOWN | wxDP_SHOWCENTURY).") - wxT("Also note that wxPG_ALLOW_WXADV needs to be defined inorder to use wxDatePickerCtrl.") ); + wxT("Attribute wxPG_DATE_PICKER_STYLE has been set to (long)") + wxT("(wxDP_DROPDOWN | wxDP_SHOWCENTURY | wxDP_ALLOWNONE).") ); #endif #endif diff --git a/src/propgrid/advprops.cpp b/src/propgrid/advprops.cpp index 83d392b310..f1af26b9da 100644 --- a/src/propgrid/advprops.cpp +++ b/src/propgrid/advprops.cpp @@ -457,7 +457,7 @@ wxPGWindowList wxPGDatePickerCtrlEditor::CreateControls( wxPropertyGrid* propgri NULL, wxT("DatePickerCtrl editor can only be used with wxDateProperty or derivative.") ); - wxDateProperty* prop = (wxDateProperty*) property; + wxDateProperty* prop = wxDynamicCast(property, wxDateProperty); // Use two stage creation to allow cleaner display on wxMSW wxDatePickerCtrl* ctrl = new wxDatePickerCtrl(); @@ -490,14 +490,18 @@ wxPGWindowList wxPGDatePickerCtrlEditor::CreateControls( wxPropertyGrid* propgri } // Copies value from property to control -void wxPGDatePickerCtrlEditor::UpdateControl( wxPGProperty* property, wxWindow* wnd ) const +void wxPGDatePickerCtrlEditor::UpdateControl( wxPGProperty* property, + wxWindow* wnd ) const { wxDatePickerCtrl* ctrl = (wxDatePickerCtrl*) wnd; wxASSERT( ctrl && ctrl->IsKindOf(CLASSINFO(wxDatePickerCtrl)) ); - // We assume that property's data type is 'int' (or something similar), - // thus allowing us to get raw, unchecked value via DoGetValue. - ctrl->SetValue( property->GetValue().GetDateTime() ); + wxDateTime dateValue(wxInvalidDateTime); + wxVariant v(property->GetValue()); + if ( v.GetType() == wxT("datetime") ) + dateValue = v.GetDateTime(); + + ctrl->SetValue( dateValue ); } // Control's events are redirected here @@ -522,11 +526,20 @@ bool wxPGDatePickerCtrlEditor::GetValueFromControl( wxVariant& variant, wxPGProp return true; } -void wxPGDatePickerCtrlEditor::SetValueToUnspecified( wxPGProperty* WXUNUSED(property), wxWindow* WXUNUSED(wnd) ) const +void wxPGDatePickerCtrlEditor::SetValueToUnspecified( wxPGProperty* property, + wxWindow* wnd ) const { - // TODO? - //wxDateProperty* prop = (wxDateProperty*) property; - //ctrl->SetValue(?); + wxDatePickerCtrl* ctrl = (wxDatePickerCtrl*) wnd; + wxASSERT( ctrl && ctrl->IsKindOf(CLASSINFO(wxDatePickerCtrl)) ); + + wxDateProperty* prop = wxDynamicCast(property, wxDateProperty); + + if ( prop ) + { + int datePickerStyle = prop->GetDatePickerStyle(); + if ( datePickerStyle & wxDP_ALLOWNONE ) + ctrl->SetValue(wxInvalidDateTime); + } } #endif // wxUSE_DATEPICKCTRL @@ -2066,6 +2079,17 @@ wxDateProperty::~wxDateProperty() { } +void wxDateProperty::OnSetValue() +{ + // + // Convert invalid dates to unspecified value + if ( m_value.GetType() == wxT("datetime") ) + { + if ( !m_value.GetDateTime().IsValid() ) + m_value.MakeNull(); + } +} + bool wxDateProperty::StringToValue( wxVariant& variant, const wxString& text, int WXUNUSED(argFlags) ) const { -- 2.45.2