From 9b5bafcf38a1c6b1ad9f8974e4745c73a559a26e Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Wed, 8 Oct 2008 18:15:10 +0000 Subject: [PATCH 1/1] Removed dysfunctional wxPGPropery::PrepareValueForDialogEditing(); Replaced its functionality with wxPropertyGrid::GetPendingEditedValue(); Added wxPropertyGrid::PerformValidation() flags so it can be called in generic context. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56169 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/propgrid/property.h | 17 ------ include/wx/propgrid/propgrid.h | 29 +++++++--- interface/wx/propgrid/property.h | 21 -------- interface/wx/propgrid/propgrid.h | 8 +++ samples/propgrid/sampleprops.cpp | 11 ++-- src/propgrid/advprops.cpp | 8 +-- src/propgrid/property.cpp | 6 --- src/propgrid/propgrid.cpp | 93 ++++++++++++++++++++++---------- src/propgrid/props.cpp | 10 ++-- 9 files changed, 110 insertions(+), 93 deletions(-) diff --git a/include/wx/propgrid/property.h b/include/wx/propgrid/property.h index ece85011db..6825cda445 100644 --- a/include/wx/propgrid/property.h +++ b/include/wx/propgrid/property.h @@ -1814,23 +1814,6 @@ public: } #endif // #if wxUSE_VALIDATORS - /** Updates property value in case there were last minute - changes. If value was unspecified, it will be set to default. - Use only for properties that have TextCtrl-based editor. - @remarks - If you have code similar to - @code - // Update the value in case of last minute changes - if ( primary && propgrid->IsEditorsValueModified() ) - GetEditorClass()->CopyValueFromControl( this, primary ); - @endcode - in wxPGProperty::OnEvent wxEVT_COMMAND_BUTTON_CLICKED handler, - then replace it with call to this method. - @return - True if value changed. - */ - bool PrepareValueForDialogEditing( wxPropertyGrid* propgrid ); - #ifndef SWIG /** Returns client data (void*) of a property. */ diff --git a/include/wx/propgrid/propgrid.h b/include/wx/propgrid/propgrid.h index 8bad63c5d4..3796b332d3 100644 --- a/include/wx/propgrid/propgrid.h +++ b/include/wx/propgrid/propgrid.h @@ -832,6 +832,14 @@ public: /** Returns background colour of margin. */ wxColour GetMarginColour() const { return m_colMargin; } + /** + Returns most up-to-date value of selected property. This will return + value different from GetSelectedProperty()->GetValue() only when text + editor is activate and string edited by user represents valid, + uncommitted property value. + */ + wxVariant GetPendingEditedValue(); + /** Returns cell background colour of a property. */ wxColour GetPropertyBackgroundColour( wxPGPropArg id ) const; @@ -1257,12 +1265,6 @@ public: virtual bool DoPropertyChanged( wxPGProperty* p, unsigned int selFlags = 0 ); - /** - Runs all validation functionality (includes sending wxEVT_PG_CHANGING). - Returns true if all tests passed. - */ - virtual bool PerformValidation( wxPGProperty* p, wxVariant& pendingValue ); - /** Called when validation for given property fails. @param invalidValue Value which failed in validation. @@ -1340,6 +1342,21 @@ protected: */ virtual wxPropertyGridPageState* CreateState() const; + enum PerformValidationFlags + { + SendEvtChanging = 0x0001, + IsStandaloneValidation = 0x0002 // Not called in response to event + }; + + /** + Runs all validation functionality (includes sending wxEVT_PG_CHANGING). + Returns true if all tests passed. Implement in derived class to + add additional validation behavior. + */ + virtual bool PerformValidation( wxPGProperty* p, + wxVariant& pendingValue, + int flags = SendEvtChanging ); + #ifndef DOXYGEN public: diff --git a/interface/wx/propgrid/property.h b/interface/wx/propgrid/property.h index 229a8ede3c..5b5e500e92 100644 --- a/interface/wx/propgrid/property.h +++ b/interface/wx/propgrid/property.h @@ -240,9 +240,6 @@ @code virtual bool OnButtonClick( wxPropertyGrid* propGrid, wxString& value ) { - // Update property value from editor, if necessary - PrepareValueForDialogEditing(propGrid); - wxSize dialogSize(...size of your dialog...); wxPoint dlgPos = propGrid->GetGoodEditorDialogPosition(this, @@ -1199,24 +1196,6 @@ public: */ wxPGProperty* Item( size_t i ) const; - /** - Updates property value in case there were last minute - changes. If value was unspecified, it will be set to default. - Use only for properties that have TextCtrl-based editor. - - @remarks If you have code similar to - @code - // Update the value in case of last minute changes - if ( primary && propgrid->IsEditorsValueModified() ) - GetEditorClass()->CopyValueFromControl( this, primary ); - @endcode - in wxPGProperty::OnEvent wxEVT_COMMAND_BUTTON_CLICKED handler, - then replace it with call to this method. - - @return Returns @true if value changed. - */ - bool PrepareValueForDialogEditing( wxPropertyGrid* propgrid ); - /** If property's editor is active, then update it's value. */ diff --git a/interface/wx/propgrid/propgrid.h b/interface/wx/propgrid/propgrid.h index 8e96de4352..bcbf41ac7d 100644 --- a/interface/wx/propgrid/propgrid.h +++ b/interface/wx/propgrid/propgrid.h @@ -586,6 +586,14 @@ public: */ wxColour GetMarginColour() const; + /** + Returns most up-to-date value of selected property. This will return + value different from GetSelectedProperty()->GetValue() only when text + editor is activate and string edited by user represents valid, + uncommitted property value. + */ + wxVariant GetPendingEditedValue(); + /** Returns cell background colour of a property. */ diff --git a/samples/propgrid/sampleprops.cpp b/samples/propgrid/sampleprops.cpp index 459de82c47..21303e7af4 100644 --- a/samples/propgrid/sampleprops.cpp +++ b/samples/propgrid/sampleprops.cpp @@ -131,11 +131,10 @@ bool wxFontDataProperty::OnEvent( wxPropertyGrid* propgrid, { if ( propgrid->IsMainButtonEvent(event) ) { - // Update value from last minute changes - PrepareValueForDialogEditing(propgrid); + wxVariant useValue = propgrid->GetPendingEditedValue(); wxFontData fontData; - fontData << m_value_wxFontData; + fontData << useValue; fontData.SetInitialFont(fontData.GetChosenFont()); @@ -554,10 +553,10 @@ bool wxArrayDoubleProperty::OnEvent( wxPropertyGrid* propgrid, { if ( propgrid->IsMainButtonEvent(event) ) { - wxArrayDouble& value = wxArrayDoubleRefFromVariant(m_value); - // Update the value in case of last minute changes - PrepareValueForDialogEditing(propgrid); + wxVariant useValue = propgrid->GetPendingEditedValue(); + + wxArrayDouble& value = wxArrayDoubleRefFromVariant(useValue); // Create editor dialog. wxArrayDoubleEditorDialog dlg; diff --git a/src/propgrid/advprops.cpp b/src/propgrid/advprops.cpp index 1f556ddbcf..f50ce38f49 100644 --- a/src/propgrid/advprops.cpp +++ b/src/propgrid/advprops.cpp @@ -531,11 +531,11 @@ bool wxFontProperty::OnEvent( wxPropertyGrid* propgrid, wxWindow* WXUNUSED(prima if ( propgrid->IsMainButtonEvent(event) ) { // Update value from last minute changes - PrepareValueForDialogEditing(propgrid); + wxVariant useValue = propgrid->GetPendingEditedValue(); wxFontData data; wxFont font; - font << m_value; + font << useValue; data.SetInitialFont( font ); data.SetColour(*wxBLACK); @@ -1803,7 +1803,7 @@ bool wxMultiChoiceProperty::OnEvent( wxPropertyGrid* propgrid, if ( propgrid->IsMainButtonEvent(event) ) { // Update the value - PrepareValueForDialogEditing(propgrid); + wxVariant useValue = propgrid->GetPendingEditedValue(); wxArrayString labels = m_choices.GetLabels(); unsigned int choiceCount; @@ -1823,7 +1823,7 @@ bool wxMultiChoiceProperty::OnEvent( wxPropertyGrid* propgrid, dlg.Move( propgrid->GetGoodEditorDialogPosition(this,dlg.GetSize()) ); - wxArrayString strings = m_value.GetArrayString(); + wxArrayString strings = useValue.GetArrayString(); wxArrayString extraStrings; dlg.SetSelections(m_choices.GetIndicesForStrings(strings, &extraStrings)); diff --git a/src/propgrid/property.cpp b/src/propgrid/property.cpp index cc14b53a9f..e26512862e 100644 --- a/src/propgrid/property.cpp +++ b/src/propgrid/property.cpp @@ -1412,12 +1412,6 @@ bool wxPGProperty::HasVisibleChildren() const return false; } -bool wxPGProperty::PrepareValueForDialogEditing( wxPropertyGrid* propGrid ) -{ - return propGrid->CommitChangesFromEditor(); -} - - bool wxPGProperty::RecreateEditor() { wxPropertyGrid* pg = GetGrid(); diff --git a/src/propgrid/propgrid.cpp b/src/propgrid/propgrid.cpp index 0b0f8a4bcf..0ec16d26c2 100644 --- a/src/propgrid/propgrid.cpp +++ b/src/propgrid/propgrid.cpp @@ -2731,7 +2731,8 @@ bool wxPropertyGrid::CommitChangesFromEditor( wxUint32 flags ) // ----------------------------------------------------------------------- -bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue ) +bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue, + int flags ) { // // Runs all validation functionality. @@ -2800,34 +2801,37 @@ bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue wxVariant evtChangingValue = value; - // FIXME: After proper ValueToString()s added, remove - // this. It is just a temporary fix, as evt_changing - // will simply not work for wxPG_PROP_COMPOSED_VALUE - // (unless it is selected, and textctrl editor is open). - if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) ) + if ( flags & SendEvtChanging ) { - evtChangingProperty = baseChangedProperty; - if ( evtChangingProperty != p ) + // FIXME: After proper ValueToString()s added, remove + // this. It is just a temporary fix, as evt_changing + // will simply not work for wxPG_PROP_COMPOSED_VALUE + // (unless it is selected, and textctrl editor is open). + if ( changedProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) ) { - evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue ); - } - else - { - evtChangingValue = pendingValue; + evtChangingProperty = baseChangedProperty; + if ( evtChangingProperty != p ) + { + evtChangingProperty->AdaptListToValue( bcpPendingList, &evtChangingValue ); + } + else + { + evtChangingValue = pendingValue; + } } - } - if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) ) - { - if ( changedProperty == m_selected ) - { - wxWindow* editor = GetEditorControl(); - wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) ); - evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue(); - } - else + if ( evtChangingProperty->HasFlag(wxPG_PROP_COMPOSED_VALUE) ) { - wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value.")); + if ( changedProperty == m_selected ) + { + wxWindow* editor = GetEditorControl(); + wxASSERT( editor->IsKindOf(CLASSINFO(wxTextCtrl)) ); + evtChangingValue = wxStaticCast(editor, wxTextCtrl)->GetValue(); + } + else + { + wxLogDebug(wxT("WARNING: wxEVT_PG_CHANGING is about to happen with old value.")); + } } } @@ -2849,9 +2853,20 @@ bool wxPropertyGrid::PerformValidation( wxPGProperty* p, wxVariant& pendingValue return false; } - // SendEvent returns true if event was vetoed - if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty, &evtChangingValue, 0 ) ) - return false; + if ( flags & SendEvtChanging ) + { + // SendEvent returns true if event was vetoed + if ( SendEvent( wxEVT_PG_CHANGING, evtChangingProperty, &evtChangingValue, 0 ) ) + return false; + } + + if ( flags & IsStandaloneValidation ) + { + // If called in 'generic' context, we need to reset + // m_chgInfo_changedProperty and write back translated value. + m_chgInfo_changedProperty = NULL; + pendingValue = value; + } return true; } @@ -3089,6 +3104,30 @@ bool wxPropertyGrid::ChangePropertyValue( wxPGPropArg id, wxVariant newValue ) // ----------------------------------------------------------------------- +wxVariant wxPropertyGrid::GetPendingEditedValue() +{ + wxPGProperty* prop = GetSelectedProperty(); + + if ( !prop ) + return wxNullVariant; + + wxTextCtrl* tc = GetEditorTextCtrl(); + wxVariant value = prop->GetValue(); + + if ( !tc || !IsEditorsValueModified() ) + return value; + + if ( !prop->StringToValue(value, tc->GetValue()) ) + return value; + + if ( !PerformValidation(prop, value, IsStandaloneValidation) ) + return prop->GetValue(); + + return value; +} + +// ----------------------------------------------------------------------- + // Runs wxValidator for the selected property bool wxPropertyGrid::DoEditorValidate() { diff --git a/src/propgrid/props.cpp b/src/propgrid/props.cpp index d4e078bd2d..a411916687 100644 --- a/src/propgrid/props.cpp +++ b/src/propgrid/props.cpp @@ -1558,8 +1558,6 @@ wxValidator* wxDirProperty::DoGetValidator() const bool wxDirProperty::OnButtonClick( wxPropertyGrid* propGrid, wxString& value ) { // Update property value from editor, if necessary - PrepareValueForDialogEditing(propGrid); - wxSize dlg_sz(300,400); wxDirDialog dlg( propGrid, @@ -1887,9 +1885,9 @@ bool wxLongStringProperty::OnEvent( wxPropertyGrid* propGrid, wxWindow* WXUNUSED if ( propGrid->IsMainButtonEvent(event) ) { // Update the value - PrepareValueForDialogEditing(propGrid); + wxVariant useValue = propGrid->GetPendingEditedValue(); - wxString val1 = GetValueAsString(0); + wxString val1 = useValue.GetString(); wxString val_orig = val1; wxString value; @@ -2489,7 +2487,7 @@ bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid* propGrid, const wxChar* cbt ) { // Update the value - PrepareValueForDialogEditing(propGrid); + wxVariant useValue = propGrid->GetPendingEditedValue(); if ( !propGrid->EditorValidate() ) return false; @@ -2506,7 +2504,7 @@ bool wxArrayStringProperty::OnButtonClick( wxPropertyGrid* propGrid, if ( strEdDlg ) strEdDlg->SetCustomButton(cbt, this); - dlg->SetDialogValue( wxVariant(m_value) ); + dlg->SetDialogValue( useValue ); dlg->Create(propGrid, wxEmptyString, m_label); #if !wxPG_SMALL_SCREEN -- 2.45.2