Skip to content

Commit

Permalink
Merge pull request #1569 from KeyWorksRW/project_handler
Browse files Browse the repository at this point in the history
Additional switching to wxFileName for file/directory handling
  • Loading branch information
Randalphwa authored Jan 26, 2025
2 parents b2629ff + da1eef3 commit 9be967a
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 218 deletions.
35 changes: 27 additions & 8 deletions src/customprops/directory_prop.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////////////////////////
// Purpose: Derived wxStringProperty class for choosing a directory
// Author: Ralph Walden
// Copyright: Copyright (c) 2021-2023 KeyWorks Software (Ralph Walden)
// Copyright: Copyright (c) 2021-2025 KeyWorks Software (Ralph Walden)
// License: Apache License -- see ../../LICENSE
/////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -34,24 +34,43 @@ bool DirectoryDialogAdapter::DoShowDialog(wxPropertyGrid* propGrid, wxPGProperty
dlg_pos = propGrid->GetGoodEditorDialogPosition(property, dlg_sz);
}

tt_string path = Project.getProjectPath();
wxFileName path;
auto node = m_prop->getNode();
if (node->isGen(gen_wxFilePickerCtrl))
{
path = m_prop->as_string().size() ? m_prop->as_string() : Project.getProjectPath();
if (m_prop->as_string().size())
{
path.AssignDir(m_prop->as_string());
}
else
{
path = *Project.get_wxFileName();
path.SetFullName(wxEmptyString); // clear the project filename
}
}
else
{
path.append_filename(m_prop->as_string());
path.make_absolute();
path = *Project.get_wxFileName();
path.SetFullName(wxEmptyString); // clear the project filename
if (m_prop->as_string().size())
{
wxFileName prop_path;
prop_path.AssignDir(m_prop->as_string());
for (auto& iter: prop_path.GetDirs())
{
path.AppendDir(iter);
}
path.MakeAbsolute();
}
}

// If the directory doesn't exist, then we need to reset it. Otherwise on Windows, the
// dialog will be for the computer, requiring the user to drill down to where the project
// file is.
if (!node->isGen(gen_wxFilePickerCtrl) && !path.dir_exists())
if (!node->isGen(gen_wxFilePickerCtrl) && !path.DirExists())
{
path = Project.getProjectPath();
path = *Project.get_wxFileName();
path.SetFullName(wxEmptyString); // clear the project filename
}

auto style = wxDD_DEFAULT_STYLE | wxDD_CHANGE_DIR;
Expand All @@ -60,7 +79,7 @@ bool DirectoryDialogAdapter::DoShowDialog(wxPropertyGrid* propGrid, wxPGProperty
style |= wxDD_DIR_MUST_EXIST;
}

wxDirDialog dlg(propGrid, wxDirSelectorPromptStr, path.make_wxString(), style, dlg_pos, dlg_sz);
wxDirDialog dlg(propGrid, wxDirSelectorPromptStr, path.GetPath(), style, dlg_pos, dlg_sz);
if (dlg.ShowModal() == wxID_OK)
{
SetValue(dlg.GetPath());
Expand Down
6 changes: 3 additions & 3 deletions src/customprops/img_string_prop.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////////////////////////
// Purpose: Derived wxStringProperty class for handling wxImage files or art
// Author: Ralph Walden
// Copyright: Copyright (c) 2021-2023 KeyWorks Software (Ralph Walden)
// Copyright: Copyright (c) 2021-2025 KeyWorks Software (Ralph Walden)
// License: Apache License -- see ../../LICENSE
/////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -68,9 +68,9 @@ bool ImageDialogAdapter::DoShowDialog(wxPropertyGrid* propGrid, wxPGProperty* WX
tt_cwd cwd(true);
if (Project.hasValue(prop_art_directory))
{
if (auto dir = Project.ArtDirectory(); dir.dir_exists())
if (auto dir = Project.getArtPath(); dir->DirExists())
{
wxFileName::SetCwd(dir.make_wxString());
dir->SetCwd();
}
}

Expand Down
156 changes: 76 additions & 80 deletions src/customprops/tt_file_property.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/////////////////////////////////////////////////////////////////////////////
// Purpose: Version of wxFileProperty specific to wxUiEditor
// Author: Ralph Walden
// Copyright: Copyright (c) 2024 KeyWorks Software (Ralph Walden)
// Copyright: Copyright (c) 2024-2025 KeyWorks Software (Ralph Walden)
// License: Apache License -- see ../../LICENSE
/////////////////////////////////////////////////////////////////////////////

#include <wx/filedlg.h>
#include <wx/filename.h> // wxFileName - encapsulates a file path

#include "node_prop.h" // for wxNodeProperty
#include "project_handler.h" // ProjectHandler class
Expand All @@ -29,7 +30,7 @@ ttFileProperty::ttFileProperty(const wxString& label, const wxString& name, cons

bool ttFileProperty::DisplayEditorDialog(wxPropertyGrid* pg, wxVariant& value)
{
tt_string root_path;
wxFileName root_path;
wxString wildcard;
wxString title;
auto* form = m_prop->getNode()->getForm();
Expand All @@ -39,118 +40,119 @@ bool ttFileProperty::DisplayEditorDialog(wxPropertyGrid* pg, wxVariant& value)
{
case prop_base_file:
if (folder && folder->hasValue(prop_folder_base_directory))
root_path = folder->as_string(prop_folder_base_directory);
root_path.AssignDir(folder->as_string(prop_folder_base_directory));
else if (Project.getProjectNode()->hasValue(prop_base_directory))
root_path = Project.getProjectNode()->as_string(prop_base_directory);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_base_directory));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Base class filename";
wildcard = "C++ Files|*.cpp;*.cc;*.cxx";
break;

case prop_derived_file:

if (folder && folder->hasValue(prop_folder_derived_directory))
root_path = folder->as_string(prop_folder_derived_directory);
root_path.AssignDir(folder->as_string(prop_folder_derived_directory));
else if (Project.getProjectNode()->hasValue(prop_derived_directory))
root_path = Project.getProjectNode()->as_string(prop_derived_directory);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_derived_directory));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Derived class filename";
wildcard = "C++ Files|*.cpp;*.cc;*.cxx";
break;

case prop_xrc_file:
case prop_combined_xrc_file:
case prop_folder_combined_xrc_file:
if (folder && folder->hasValue(prop_folder_xrc_directory))
root_path = folder->as_string(prop_folder_xrc_directory);
else if (Project.getProjectNode()->hasValue(prop_xrc_directory))
root_path = Project.getProjectNode()->as_string(prop_xrc_directory);
case prop_perl_file:
if (folder && folder->hasValue(prop_folder_perl_output_folder))
root_path.AssignDir(folder->as_string(prop_folder_perl_output_folder));
else if (Project.getProjectNode()->hasValue(prop_perl_output_folder))
root_path.AssignDir(Project.getProjectNode()->as_string(prop_perl_output_folder));
else
root_path = Project.getProjectPath();
title = "XRC filename";
wildcard = "XRC Files|*.xrc";
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Perl filename";
wildcard = "Perl Files|*.pl;*.pm";
break;

case prop_python_file:
case prop_python_combined_file:
if (folder && folder->hasValue(prop_folder_python_output_folder))
root_path = folder->as_string(prop_folder_python_output_folder);
root_path.AssignDir(folder->as_string(prop_folder_python_output_folder));
else if (Project.getProjectNode()->hasValue(prop_python_output_folder))
root_path = Project.getProjectNode()->as_string(prop_python_output_folder);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_python_output_folder));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Python filename";
wildcard = "Python Files|*.py";
break;

case prop_ruby_file:
case prop_ruby_combined_file:
if (folder && folder->hasValue(prop_folder_ruby_output_folder))
root_path = folder->as_string(prop_folder_ruby_output_folder);
root_path.AssignDir(folder->as_string(prop_folder_ruby_output_folder));
else if (Project.getProjectNode()->hasValue(prop_ruby_output_folder))
root_path = Project.getProjectNode()->as_string(prop_ruby_output_folder);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_ruby_output_folder));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Ruby filename";
wildcard = "Ruby Files|*.rb;*.rbw";
break;

case prop_rust_file:
if (folder && folder->hasValue(prop_folder_rust_output_folder))
root_path.AssignDir(folder->as_string(prop_folder_rust_output_folder));
else if (Project.getProjectNode()->hasValue(prop_rust_output_folder))
root_path.AssignDir(Project.getProjectNode()->as_string(prop_rust_output_folder));
else
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Rust filename";
wildcard = "Rust Files|*.rust";
break;

case prop_xrc_file:
case prop_combined_xrc_file:
case prop_folder_combined_xrc_file:
if (folder && folder->hasValue(prop_folder_xrc_directory))
root_path.AssignDir(folder->as_string(prop_folder_xrc_directory));
else if (Project.getProjectNode()->hasValue(prop_xrc_directory))
root_path.AssignDir(Project.getProjectNode()->as_string(prop_xrc_directory));
else
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "XRC filename";
wildcard = "XRC Files|*.xrc";
break;

#if GENERATE_NEW_LANG_CODE
case prop_fortran_file:
if (folder && folder->hasValue(prop_folder_fortran_output_folder))
root_path = folder->as_string(prop_folder_fortran_output_folder);
root_path.AssignDir(folder->as_string(prop_folder_fortran_output_folder));
else if (Project.getProjectNode()->hasValue(prop_fortran_output_folder))
root_path = Project.getProjectNode()->as_string(prop_fortran_output_folder);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_fortran_output_folder));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Fortran filename";
wildcard = "Fortran Files|*.f90;*.f95;*.f03;*.f08";
break;

case prop_haskell_file:
if (folder && folder->hasValue(prop_folder_haskell_output_folder))
root_path = folder->as_string(prop_folder_haskell_output_folder);
root_path.AssignDir(folder->as_string(prop_folder_haskell_output_folder));
else if (Project.getProjectNode()->hasValue(prop_haskell_output_folder))
root_path = Project.getProjectNode()->as_string(prop_haskell_output_folder);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_haskell_output_folder));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Haskell filename";
wildcard = "Haskell Files|*.hs;*.lhs";
break;

case prop_lua_file:
if (folder && folder->hasValue(prop_folder_lua_output_folder))
root_path = folder->as_string(prop_folder_lua_output_folder);
root_path.AssignDir(folder->as_string(prop_folder_lua_output_folder));
else if (Project.getProjectNode()->hasValue(prop_lua_output_folder))
root_path = Project.getProjectNode()->as_string(prop_lua_output_folder);
root_path.AssignDir(Project.getProjectNode()->as_string(prop_lua_output_folder));
else
root_path = Project.getProjectPath();
root_path.AssignDir(Project.get_wxFileName()->GetPath());
title = "Lua filename";
wildcard = "Lua Files|*.lua";
break;

case prop_perl_file:
if (folder && folder->hasValue(prop_folder_perl_output_folder))
root_path = folder->as_string(prop_folder_perl_output_folder);
else if (Project.getProjectNode()->hasValue(prop_perl_output_folder))
root_path = Project.getProjectNode()->as_string(prop_perl_output_folder);
else
root_path = Project.getProjectPath();
title = "Perl filename";
wildcard = "Perl Files|*.pl;*.pm";
break;

case prop_rust_file:
if (folder && folder->hasValue(prop_folder_rust_output_folder))
root_path = folder->as_string(prop_folder_rust_output_folder);
else if (Project.getProjectNode()->hasValue(prop_rust_output_folder))
root_path = Project.getProjectNode()->as_string(prop_rust_output_folder);
else
root_path = Project.getProjectPath();
title = "Rust filename";
wildcard = "Rust Files|*.rust";
break;
#endif // GENERATE_NEW_LANG_CODE

case prop_cmake_file:
case prop_folder_cmake_file:
Expand All @@ -175,15 +177,15 @@ bool ttFileProperty::DisplayEditorDialog(wxPropertyGrid* pg, wxVariant& value)
case prop_data_file:
if (m_prop->as_string().size())
{
root_path = m_prop->as_string();
root_path.remove_filename();
root_path.Assign(m_prop->as_string());
}
else
{
auto result = Project.GetOutputPath(m_prop->getNode()->getForm(), GEN_LANG_CPLUSPLUS);
root_path = result.first;
if (result.second) // true if the the base filename was returned
root_path.remove_filename();
if (!result.second)
root_path.AssignDir(result.first);
else
root_path.Assign(result.first);
}
if (m_prop->getNode()->isGen(gen_data_xml))
{
Expand All @@ -203,30 +205,24 @@ bool ttFileProperty::DisplayEditorDialog(wxPropertyGrid* pg, wxVariant& value)

} // switch (m_prop->get_name())

tt_string full_path = root_path;
auto cur_path = value.GetString().utf8_string();
wxFileName full_path;
full_path.AssignDir(root_path.GetPath());
auto cur_path = value.GetString();
if (cur_path.starts_with("./"))
{
full_path = cur_path;
}
else
{
full_path.append_filename(cur_path);
full_path.Assign(cur_path);
}
full_path.make_absolute();
wxString filename = full_path.filename().make_wxString();
full_path.remove_filename();
wxFileDialog dlg(pg->GetPanel(), title, full_path.make_wxString(), filename, wildcard, wxFD_SAVE);
full_path.MakeAbsolute();
wxFileDialog dlg(pg->GetPanel(), title, full_path.GetPath(), full_path.GetFullName(), wildcard, wxFD_SAVE);
if (dlg.ShowModal() == wxID_OK)
{
full_path = dlg.GetPath().utf8_string();
// Note that no matter whether the filename is in one of the base or output directories, we
// always make it relative to the project path.
full_path.make_relative(Project.getProjectPath());
full_path.backslashestoforward();
if (!full_path.contains("/"))
full_path = "./" + full_path;
value = full_path.make_wxString();
full_path.Assign(dlg.GetPath());
full_path.MakeRelativeTo(Project.get_wxFileName()->GetPath());
tt_string final_path = full_path.GetFullPath().utf8_string();
final_path.backslashestoforward();
if (!final_path.contains("/"))
final_path = "./" + full_path.GetFullPath().utf8_string();
value = final_path.make_wxString();
return true;
}
return false;
Expand Down
Loading

0 comments on commit 9be967a

Please sign in to comment.