1
0
forked from cheng/wallet
wallet/samples/config/frame.cpp
Cheng d59729f396
Never really figured out why my code was breaking
fixed it by looking for funny things that deviated from
the sameples,  and doing various recommended safe things,
and found a few sql errors, and one by one the crashes
went away.

The new wxWidgets just seems less tolerant of little careless
stuff that is not right.
2023-10-18 20:23:56 +10:00

162 lines
4.9 KiB
C++

#include "stdafx.h"
void Frame::RestorePositionFromConfig(const wxSize& bestSize) {
wxConfigBase* pConfig = wxConfigBase::Get();
if (pConfig) {
// SetPath() understands ".." but you should probably never use it.
pConfig->SetPath(wxT("/MainFrame")); wxPoint scr{ wxSystemSettings::GetMetric(wxSYS_SCREEN_X), wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) };
// restore frame position and size
int x = pConfig->ReadLong(wxT("x"), scr.x / 4);
int y = pConfig->ReadLong(wxT("y"), scr.y / 4);
int w = pConfig->ReadLong(wxT("w"), scr.x / 2);
int h = pConfig->ReadLong(wxT("h"), scr.y / 2);
w = std::min(std::max(std::max(w, scr.x / 5), bestSize.GetWidth()), 8 * scr.x / 9);
h = std::min(std::max(std::max(h, scr.y / 9), bestSize.GetHeight()), 4 * scr.y / 5);
x = std::max(scr.x / 12, std::min(x, scr.x - w - scr.x / 12));
y = std::max(scr.y / 10, std::min(y, scr.y - h - scr.y / 10));
this->Move(x, y);
this->Maximize(pConfig->ReadBool(wxT("Maximized"), false));
this->SetSize(w, h);
pConfig->SetPath(wxT("/"));
}
}
void Frame::StorePositionToConfig() {
wxConfigBase* pConfig = wxConfigBase::Get();
if (pConfig) {
pConfig->SetPath(wxT("/MainFrame"));
if (this->IsMaximized()) {
pConfig->Write(wxT("Maximized"), true);
}
else {
// save the frame position
int x, y, w, h;
this->GetSize(&w, &h);
this->GetPosition(&x, &y);
pConfig->Write(wxT("x"), (long)x);
pConfig->Write(wxT("y"), (long)y);
pConfig->Write(wxT("w"), (long)w);
pConfig->Write(wxT("h"), (long)h);
pConfig->Write(wxT("Maximized"), false);
}
pConfig->SetPath(wxT("/"));
}
}
// main frame ctor
Frame::Frame()
: wxFrame((wxFrame*)NULL, wxID_ANY, "wxConfig Demo")
{
SetIcon(wxICON(sample));
// menu
wxMenu* menuFile = new wxMenu;
menuFile->Append(wxID_DELETE, "&Delete", "Delete config file");
menuFile->Bind(wxEVT_MENU, &Frame::OnQuit, this, wxID_EXIT);
menuFile->AppendSeparator();
menuFile->Append(wxID_ABOUT, "&About\tF1", "About this sample");
menuFile->Bind(wxEVT_MENU, &Frame::OnAbout, this, wxID_ABOUT);
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT, "E&xit\tAlt-X", "Exit the program");
menuFile->Bind(wxEVT_MENU, &Frame::OnDelete, this, wxID_DELETE);
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
SetMenuBar(menuBar);
#if wxUSE_STATUSBAR
CreateStatusBar();
#endif // wxUSE_STATUSBAR
// child controls
wxPanel* panel = new wxPanel(this);
wxStaticText* st = new wxStaticText(panel, wxID_ANY, "These controls remember their values!");
m_text = new wxTextCtrl(panel, wxID_ANY);
m_check = new wxCheckBox(panel, wxID_ANY, "show welcome message box at startup");
// put everything in a sizer
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(st, wxSizerFlags().Border(wxLEFT | wxBOTTOM | wxTOP, 10));
sizer->Add(m_text, wxSizerFlags().Border(wxLEFT | wxBOTTOM | wxRIGHT, 10).Expand());
sizer->Add(m_check, wxSizerFlags().Border(wxLEFT, 10));
panel->SetSizer(sizer);
// restore the control's values from the config
// NB: in this program, the config object is already created at this moment
// because we had called Get() from MyApp::OnInit(). However, if you later
// change the code and don't create it before this line, it won't break
// anything - unlike if you manually create wxConfig object with Create()
// or in any other way (then you must be sure to create it before using it!).
wxConfigBase* pConfig = wxConfigBase::Get();
// we could write Read("/Controls/Text") as well, it's just to show SetPath()
pConfig->SetPath("/Controls");
m_text->SetValue(pConfig->Read("Text", ""));
m_check->SetValue(pConfig->Read("Check", 1l) != 0);
pConfig->SetPath("/");
wxString s;
if (pConfig->Read("TestValue", &s))
{
wxLogStatus(this, "TestValue from config is '%s'", s);
}
else
{
wxLogStatus(this, "TestValue not found in the config");
}
this->RestorePositionFromConfig(ClientToWindowSize(panel->GetBestSize()));
SetClientSize(GetClientSize());
}
void Frame::OnQuit(wxCommandEvent&)
{
Close(true);
}
void Frame::OnAbout(wxCommandEvent&)
{
wxMessageBox("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin", "About",
wxICON_INFORMATION | wxOK);
}
void Frame::OnDelete(wxCommandEvent&)
{
wxConfigBase* pConfig = wxConfigBase::Get();
if (pConfig == NULL)
{
wxLogError("No config to delete!");
return;
}
if (pConfig->DeleteAll())
{
wxLogMessage("Config file/registry key successfully deleted.");
delete wxConfigBase::Set(NULL);
wxConfigBase::DontCreateOnDemand();
}
else
{
wxLogError("Deleting config file/registry key failed.");
}
}
Frame::~Frame()
{
wxConfigBase* pConfig = wxConfigBase::Get();
if (pConfig == NULL)
return;
// save the control's values to the config
auto text = m_text->GetValue();
std::string str = m_text->GetValue().utf8_string();
pConfig->Write("/Controls/Text", str.c_str());
pConfig->Write("/Controls/Check", m_check->GetValue());
StorePositionToConfig();
pConfig->Write("/TestValue", "A test value");
}