From baaba4277647aa907514359bd2abc752088c340c Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sun, 12 May 2019 11:41:22 +0200 Subject: [PATCH] Refactor: display file selector dialog from within member function in wxFileProperty By moving the code to display file selector dialog from wxPGFileDialogAdapter to wxFileProperty we can encapsulate the operation of showing the dialog because all required parameters are stored in the corresponding data members and there is no need to use call generic GetAttribute() function to retrieve them. This also helps in making wxFileProperty attributes built-ones in the future. --- include/wx/propgrid/props.h | 2 ++ interface/wx/propgrid/props.h | 2 ++ src/propgrid/props.cpp | 67 ++++++++++++++++++----------------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/include/wx/propgrid/props.h b/include/wx/propgrid/props.h index ea2dbe33be..2ea1fe6413 100644 --- a/include/wx/propgrid/props.h +++ b/include/wx/propgrid/props.h @@ -584,6 +584,8 @@ public: wxFileName GetFileName() const; protected: + bool DisplayEditorDialog(wxPropertyGrid* propGrid, wxString& value); + wxString m_wildcard; wxString m_basePath; // If set, then show path relative to it wxString m_initialPath; // If set, start the file dialog here diff --git a/interface/wx/propgrid/props.h b/interface/wx/propgrid/props.h index d99e63b52a..4a40e72386 100644 --- a/interface/wx/propgrid/props.h +++ b/interface/wx/propgrid/props.h @@ -548,6 +548,8 @@ public: wxFileName GetFileName() const; protected: + bool DisplayEditorDialog(wxPropertyGrid* propGrid, wxString& value); + wxString m_wildcard; wxString m_basePath; wxString m_initialPath; diff --git a/src/propgrid/props.cpp b/src/propgrid/props.cpp index 915356ff8d..17bf88bda3 100644 --- a/src/propgrid/props.cpp +++ b/src/propgrid/props.cpp @@ -1913,43 +1913,16 @@ bool wxDirProperty::DoSetAttribute( const wxString& name, wxVariant& value ) bool wxPGFileDialogAdapter::DoShowDialog( wxPropertyGrid* propGrid, wxPGProperty* property ) { - wxString path; - int indFilter = -1; + wxFileProperty* prop = wxDynamicCast(property, wxFileProperty); + wxCHECK_MSG( prop, false, "Function called for incompatible property" ); - wxFileProperty* fileProp = wxDynamicCast(property, wxFileProperty); - if ( fileProp ) + wxString value = prop->GetValueAsString(0); + if ( prop->DisplayEditorDialog(propGrid, value) ) { - wxFileName filename = fileProp->GetValue().GetString(); - path = filename.GetPath(); - indFilter = fileProp->m_indFilter; - - if ( path.empty() && !fileProp->m_basePath.empty() ) - path = fileProp->m_basePath; - } - else - { - wxFileName fn(property->GetValue().GetString()); - path = fn.GetPath(); - } - - wxFileDialog dlg( propGrid->GetPanel(), - property->GetAttribute(wxPG_FILE_DIALOG_TITLE, _("Choose a file")), - property->GetAttribute(wxPG_FILE_INITIAL_PATH, path), - wxEmptyString, - property->GetAttribute(wxPG_FILE_WILDCARD, wxALL_FILES), - property->GetAttributeAsLong(wxPG_FILE_DIALOG_STYLE, 0), - wxDefaultPosition ); - - if ( indFilter >= 0 ) - dlg.SetFilterIndex( indFilter ); - - if ( dlg.ShowModal() == wxID_OK ) - { - if ( fileProp ) - fileProp->m_indFilter = dlg.GetFilterIndex(); - SetValue( dlg.GetPath() ); + SetValue(value); return true; } + return false; } @@ -2156,6 +2129,34 @@ bool wxFileProperty::DoSetAttribute( const wxString& name, wxVariant& value ) return wxPGProperty::DoSetAttribute(name, value); } +bool wxFileProperty::DisplayEditorDialog(wxPropertyGrid* propGrid, wxString& value) +{ + wxFileName filename = value; + wxString path = filename.GetPath(); + + if ( path.empty() && !m_basePath.empty() ) + path = m_basePath; + + wxFileDialog dlg(propGrid->GetPanel(), + m_dlgTitle.empty() ? _("Choose a file") : m_dlgTitle, + m_initialPath.empty() ? path : m_initialPath, + value, + m_wildcard.empty() ? wxALL_FILES : m_wildcard, + m_dlgStyle, + wxDefaultPosition); + + if ( m_indFilter >= 0 ) + dlg.SetFilterIndex(m_indFilter); + + if ( dlg.ShowModal() == wxID_OK ) + { + m_indFilter = dlg.GetFilterIndex(); + value = dlg.GetPath(); + return true; + } + return false; +} + // ----------------------------------------------------------------------- // wxPGLongStringDialogAdapter // -----------------------------------------------------------------------