Process command line and configuration file in the application class.
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
#include <globals.h>
|
#include <globals.h>
|
||||||
#include "s7app.h"
|
#include "s7app.h"
|
||||||
#include <XS7.h>
|
#include <XS7.h>
|
||||||
|
#include <wx/cmdline.h>
|
||||||
|
|
||||||
////@begin XPM images
|
////@begin XPM images
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@
|
|||||||
IMPLEMENT_APP( S7App )
|
IMPLEMENT_APP( S7App )
|
||||||
////@end implement app
|
////@end implement app
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* S7App type definition
|
* S7App type definition
|
||||||
@@ -122,15 +124,16 @@ bool S7App::OnInit()
|
|||||||
translations->AddCatalog(_APPNAME_);
|
translations->AddCatalog(_APPNAME_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool res = ParseCmdLine();
|
||||||
|
if ( !res )
|
||||||
|
return res;
|
||||||
|
|
||||||
|
SetConfig();
|
||||||
XS7 * appWindow = new XS7(nullptr);
|
XS7 * appWindow = new XS7(nullptr);
|
||||||
SetTopWindow(appWindow);
|
SetTopWindow(appWindow);
|
||||||
appWindow->Show ( false );
|
appWindow->Show ( false );
|
||||||
bool res = appWindow->ParseCmdLine();
|
appWindow->Setup(m_config.get());
|
||||||
if ( res )
|
appWindow->Show();
|
||||||
{
|
|
||||||
appWindow->Setup();
|
|
||||||
appWindow->Show();
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -147,3 +150,42 @@ int S7App::OnExit()
|
|||||||
////@end S7App cleanup
|
////@end S7App cleanup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool S7App::ParseCmdLine()
|
||||||
|
{
|
||||||
|
wxCmdLineParser p;
|
||||||
|
p.SetCmdLine ( wxApp::GetInstance()->argc, wxApp::GetInstance()->argv );
|
||||||
|
p.SetSwitchChars ( _T ( "-" ) );
|
||||||
|
p.AddOption ( _T ( "c" ), wxEmptyString, _ ( "Config file tag." ) );
|
||||||
|
p.AddSwitch ( _T ( "v" ), wxEmptyString, _ ( "Show version and quit." ) );
|
||||||
|
p.AddSwitch ( _T ( "h" ), wxEmptyString, _ ( "Show help and quit." ) );
|
||||||
|
p.Parse ( false );
|
||||||
|
if ( p.Found ( _T ( "c" ) ) )
|
||||||
|
{
|
||||||
|
p.Found ( _T ( "c" ), &m_configTag );
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ( p.Found ( _T ( "h" ) ) )
|
||||||
|
{
|
||||||
|
p.Usage();
|
||||||
|
return false; //Exit code is 255, not clean.
|
||||||
|
}
|
||||||
|
if ( p.Found ( _T ( "v" ) ) )
|
||||||
|
{
|
||||||
|
cout << ( _APPNAME_ + _ ( " - version " ) + to_string(_APPVERSION_) ) << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void S7App::SetConfig()
|
||||||
|
{
|
||||||
|
const wxString configDir = wxFileConfig::GetLocalFile ( _APPNAME_, wxCONFIG_USE_SUBDIR ).GetPath();
|
||||||
|
if ( !wxFileName::Exists ( configDir ) )
|
||||||
|
wxFileName::Mkdir ( configDir );
|
||||||
|
|
||||||
|
const wxString configBaseName = m_configTag.IsEmpty()
|
||||||
|
? _APPNAME_
|
||||||
|
: _APPNAME_ + wxString("-") + m_configTag;
|
||||||
|
m_config = std::make_unique<wxFileConfig>(_APPNAME_, _T("SET"), configBaseName,
|
||||||
|
wxEmptyString, wxCONFIG_USE_SUBDIR);
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@
|
|||||||
#include "wx/image.h"
|
#include "wx/image.h"
|
||||||
////@end includes
|
////@end includes
|
||||||
|
|
||||||
|
#include "wx/config.h"
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Forward declarations
|
* Forward declarations
|
||||||
*/
|
*/
|
||||||
@@ -66,6 +68,15 @@ public:
|
|||||||
////@end S7App member variables
|
////@end S7App member variables
|
||||||
private:
|
private:
|
||||||
wxLocale m_locale;
|
wxLocale m_locale;
|
||||||
|
std::unique_ptr<wxConfig> m_config = nullptr;
|
||||||
|
/*
|
||||||
|
* An optional tag for wxConfig files. This allows using different profiles
|
||||||
|
* by specifying the -c command line option.
|
||||||
|
*/
|
||||||
|
wxString m_configTag;
|
||||||
|
|
||||||
|
bool ParseCmdLine();
|
||||||
|
void SetConfig();
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|||||||
45
XS7.cpp
45
XS7.cpp
@@ -23,48 +23,13 @@ XS7::XS7(wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint
|
|||||||
: S7(parent, id, caption, pos, size, style)
|
: S7(parent, id, caption, pos, size, style)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool XS7::ParseCmdLine()
|
void XS7::Setup(wxConfig * config)
|
||||||
{
|
{
|
||||||
wxCmdLineParser p;
|
m_config = config;
|
||||||
p.SetCmdLine ( wxApp::GetInstance()->argc, wxApp::GetInstance()->argv );
|
|
||||||
p.SetSwitchChars ( _T ( "-" ) );
|
|
||||||
p.AddOption ( _T ( "c" ), wxEmptyString, _ ( "Config file tag." ) );
|
|
||||||
p.AddSwitch ( _T ( "v" ), wxEmptyString, _ ( "Show version and quit." ) );
|
|
||||||
p.AddSwitch ( _T ( "h" ), wxEmptyString, _ ( "Show help and quit." ) );
|
|
||||||
p.Parse ( false );
|
|
||||||
if ( p.Found ( _T ( "c" ) ) )
|
|
||||||
{
|
|
||||||
p.Found ( _T ( "c" ), &m_configTag );
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ( p.Found ( _T ( "h" ) ) )
|
|
||||||
{
|
|
||||||
p.Usage();
|
|
||||||
return false; //Exit code is 255, not clean.
|
|
||||||
}
|
|
||||||
if ( p.Found ( _T ( "v" ) ) )
|
|
||||||
{
|
|
||||||
cout << ( _APPNAME_ + _ ( " - version " ) + to_string(_APPVERSION_) ) << endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void XS7::Setup()
|
|
||||||
{
|
|
||||||
const wxString configDir = wxFileConfig::GetLocalFile ( _APPNAME_, wxCONFIG_USE_SUBDIR ).GetPath();
|
|
||||||
if ( !wxFileName::Exists ( configDir ) )
|
|
||||||
wxFileName::Mkdir ( configDir );
|
|
||||||
|
|
||||||
const wxString configBaseName = m_configTag.IsEmpty()
|
|
||||||
? _APPNAME_
|
|
||||||
: _APPNAME_ + wxString("-") + m_configTag;
|
|
||||||
m_config = std::make_unique<wxFileConfig>(_APPNAME_, _T("SET"), configBaseName,
|
|
||||||
wxEmptyString, wxCONFIG_USE_SUBDIR);
|
|
||||||
TimeredStatusBar * sb = new TimeredStatusBar(this);
|
TimeredStatusBar * sb = new TimeredStatusBar(this);
|
||||||
SetStatusBar(sb);
|
SetStatusBar(sb);
|
||||||
|
|
||||||
m_insaneWidget = new XInsaneWidget(panMain, sb, m_config.get());
|
m_insaneWidget = new XInsaneWidget(panMain, sb, m_config);
|
||||||
m_insaneWidget->Setup();
|
m_insaneWidget->Setup();
|
||||||
szMain->Insert(2, m_insaneWidget, 1, wxGROW | wxALL);
|
szMain->Insert(2, m_insaneWidget, 1, wxGROW | wxALL);
|
||||||
|
|
||||||
@@ -76,7 +41,7 @@ void XS7::Setup()
|
|||||||
|
|
||||||
txtBasename->SetValidator(*MiscTools::MakeFileNameValidator(false));
|
txtBasename->SetValidator(*MiscTools::MakeFileNameValidator(false));
|
||||||
txtBasename->Bind(wxEVT_LEFT_UP, &XS7::OnAbout, this);
|
txtBasename->Bind(wxEVT_LEFT_UP, &XS7::OnAbout, this);
|
||||||
MiscTools::RestoreSizePos(m_config.get(), this, wxString("/" + wxString(_APPNAME_)));
|
MiscTools::RestoreSizePos(m_config, this, wxString("/" + wxString(_APPNAME_)));
|
||||||
|
|
||||||
S7::SetTitle(wxString(_APPNAME_) + " - version " + to_string(_APPVERSION_));
|
S7::SetTitle(wxString(_APPNAME_) + " - version " + to_string(_APPVERSION_));
|
||||||
|
|
||||||
@@ -89,7 +54,7 @@ void XS7::Setup()
|
|||||||
XS7::~XS7()
|
XS7::~XS7()
|
||||||
{
|
{
|
||||||
if (m_config)
|
if (m_config)
|
||||||
MiscTools::SaveSizePos(m_config.get(), this, wxString("/") + _APPNAME_);
|
MiscTools::SaveSizePos(m_config, this, wxString("/") + _APPNAME_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void XS7::OnDpkRepositoryChange ( wxFileDirPickerEvent& evt )
|
void XS7::OnDpkRepositoryChange ( wxFileDirPickerEvent& evt )
|
||||||
|
|||||||
12
XS7.h
12
XS7.h
@@ -22,19 +22,13 @@ public:
|
|||||||
XS7(wxWindow* parent, wxWindowID id = SYMBOL_S7_IDNAME, const wxString& caption = SYMBOL_S7_TITLE,
|
XS7(wxWindow* parent, wxWindowID id = SYMBOL_S7_IDNAME, const wxString& caption = SYMBOL_S7_TITLE,
|
||||||
const wxPoint& pos = SYMBOL_S7_POSITION, const wxSize& size = SYMBOL_S7_SIZE, long style = SYMBOL_S7_STYLE );
|
const wxPoint& pos = SYMBOL_S7_POSITION, const wxSize& size = SYMBOL_S7_SIZE, long style = SYMBOL_S7_STYLE );
|
||||||
virtual ~XS7();
|
virtual ~XS7();
|
||||||
bool ParseCmdLine();
|
|
||||||
void Setup();
|
void Setup(wxConfig * config);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<wxConfig> m_config = nullptr;
|
wxConfig * m_config = nullptr;
|
||||||
XInsaneWidget * m_insaneWidget = nullptr;
|
XInsaneWidget * m_insaneWidget = nullptr;
|
||||||
|
|
||||||
/*
|
|
||||||
* An optional tag for wxConfig files. This allows using different profiles
|
|
||||||
* by specifying the -c command line option.
|
|
||||||
*/
|
|
||||||
wxString m_configTag;
|
|
||||||
|
|
||||||
void OnDpkRepositoryChange ( wxFileDirPickerEvent& evt );
|
void OnDpkRepositoryChange ( wxFileDirPickerEvent& evt );
|
||||||
void OnDpkDoubleClick ( wxMouseEvent& evt );
|
void OnDpkDoubleClick ( wxMouseEvent& evt );
|
||||||
void OnAppKeyPressed(wxKeyEvent& evt);
|
void OnAppKeyPressed(wxKeyEvent& evt);
|
||||||
|
|||||||
Reference in New Issue
Block a user