Files
s7/Resources/Utilities/ConfigEditorPopup.cpp
Saleem Edah-Tally 7d3c61c91d Use the saved config values or application values.
ConfigEditorPopup

On creating the popup, config values were always read and used. Do this if the
application does not provide its own variables. The values are still committed
to the config file in the usual way (on control destruction). The application
may restore them to its own variables during setup.
2025-07-13 16:13:56 +02:00

151 lines
5.1 KiB
C++

/*
* File: ConfigEditorPopup.cpp
* Author: Saleem EDAH-TALLY - nmset@yandex.com
* License: CeCILL-C
* Copyright Saleem EDAH-TALLY - © 2017
*
* Created on 4 mars 2017, 19:06
*/
#include "ConfigEditorPopup.h"
#include "MiscTools.h"
ConfigEditorPopup::ConfigEditorPopup ( wxWindow * parent, wxFileConfig* config )
{
m_owner = parent;
m_config = config;
}
ConfigEditorPopup::~ConfigEditorPopup() = default;
PopupTransientWindow* ConfigEditorPopup::CreatePopup()
{
wxASSERT_MSG ( ( m_config != nullptr ),_T("CONFIG IS nullptr") );
m_popup = new PopupTransientWindow ( m_owner );
m_pan = new wxPanel ( m_popup );
m_flxsz = new wxFlexGridSizer ( 0, 2, 0, 0 );
m_pan->SetSizer ( m_flxsz );
m_flxsz->AddGrowableCol ( 1 );
m_popup->Bind ( wxEVT_DESTROY, &ConfigEditorPopup::OnPopupDestroy, this );
return m_popup;
}
wxCheckBox* ConfigEditorPopup::AddCheckBox ( const wxString& label, const wxString& configPath,
bool * clientVar)
{
wxASSERT_MSG ( ( m_config != nullptr ),_T("CONFIG IS nullptr") );
wxString * cPath = new wxString ( configPath );
wxStaticText * lbl = new wxStaticText ( m_pan, wxID_ANY, label );
m_flxsz->Add ( lbl, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 0 );
wxCheckBox * cb = new wxCheckBox ( m_pan, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxCHK_2STATE );
m_flxsz->Add ( cb, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 0 );
cb->SetValue ( (!clientVar) ? m_config->ReadBool ( configPath, false ) : *clientVar);
cb->SetClientData ( cPath );
cb->Bind ( wxEVT_DESTROY, &ConfigEditorPopup::OnControlDestroy, this );
if (clientVar)
{
cb->Bind(wxEVT_COMMAND_CHECKBOX_CLICKED, [cb, clientVar] (wxCommandEvent& evt)
{
*clientVar = cb->GetValue();
}
);
}
return cb;
}
wxSpinCtrl* ConfigEditorPopup::AddSpinCtrl ( const wxString& label, const wxString& configPath,
int * clientVar)
{
wxASSERT_MSG ( ( m_config != nullptr ),_T("CONFIG IS nullptr") );
wxString * cPath = new wxString ( configPath );
wxStaticText * lbl = new wxStaticText ( m_pan, wxID_ANY, label );
m_flxsz->Add ( lbl, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxGROW | wxALL, 0 );
wxSpinCtrl * spn = new wxSpinCtrl ( m_pan, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS | wxALIGN_RIGHT, -100, 100 );
m_flxsz->Add ( spn, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 0 );
spn->SetValue ((!clientVar) ? ( int ) m_config->ReadLong ( configPath, 0 ) : *clientVar);
spn->SetClientData ( cPath );
spn->Bind ( wxEVT_DESTROY, &ConfigEditorPopup::OnControlDestroy, this );
if (clientVar)
{
spn->Bind(wxEVT_COMMAND_SPINCTRL_UPDATED, [spn, clientVar] (wxSpinEvent& evt)
{
*clientVar = spn->GetValue();
}
);
}
return spn;
}
wxTextCtrl* ConfigEditorPopup::AddTextCtrl ( const wxString& label, const wxString& configPath,
wxString * clientVar)
{
wxASSERT_MSG ( ( m_config != nullptr ),_T("CONFIG IS nullptr") );
wxString * cPath = new wxString ( configPath );
wxStaticText * lbl = new wxStaticText ( m_pan, wxID_ANY, label );
m_flxsz->Add ( lbl, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxGROW | wxALL, 0 );
wxTextCtrl * txt = new wxTextCtrl ( m_pan, wxID_ANY );
m_flxsz->Add ( txt, 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 0 );
txt->SetValue ( (!clientVar) ? m_config->Read ( configPath, wxEmptyString ) : *clientVar );
txt->SetClientData ( cPath );
txt->Bind ( wxEVT_DESTROY, &ConfigEditorPopup::OnControlDestroy, this );
if (clientVar)
{
txt->Bind(wxEVT_COMMAND_TEXT_UPDATED, [txt, clientVar] (wxCommandEvent& evt)
{
*clientVar = txt->GetValue();
}
);
}
return txt;
}
void ConfigEditorPopup::OnControlDestroy ( wxWindowDestroyEvent& evt )
{
//cout << "ConfigEditorPopup::OnControlDestroy" << endl;
evt.Skip();
wxControl * ctrl = static_cast<wxControl*> ( evt.GetEventObject() );
if ( !ctrl ) return;
const wxString className ( ctrl->GetClassInfo()->GetClassName() );
const wxString * cPath = static_cast<wxString*> ( ctrl->GetClientData() );
if ( className ==_T("wxCheckBox") )
{
wxCheckBox * cb = static_cast<wxCheckBox*> ( evt.GetEventObject() );
m_config->Write ( *cPath, cb->GetValue() );
}
if ( className ==_T("wxSpinCtrl") )
{
wxSpinCtrl * spn = static_cast<wxSpinCtrl*> ( evt.GetEventObject() );
m_config->Write ( *cPath, spn->GetValue() );
}
if ( className ==_T("wxTextCtrl") )
{
wxTextCtrl * txt = static_cast<wxTextCtrl*> ( evt.GetEventObject() );
if ( txt->IsEmpty() )
{
m_config->DeleteEntry ( *cPath, true );
}
else
{
m_config->Write ( *cPath, txt->GetValue() );
}
}
m_config->Flush();
delete cPath;
}
void ConfigEditorPopup::OnPopupDestroy ( wxWindowDestroyEvent& evt )
{
//cout << "ConfigEditorPopup::OnPopupDestroy" << endl;
evt.Skip();
}
void ConfigEditorPopup::ShowPopup()
{
m_pan->GetSizer()->SetSizeHints ( m_pan );
MiscTools::ShowTransientPopup ( m_popup, m_pan, m_pan->GetSize().GetWidth(), m_pan->GetSize().GetHeight() );
}