1998-08-03 18:37:42 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: conftest.cpp
|
|
|
|
// Purpose: demo of wxConfig and related classes
|
|
|
|
// Author: Vadim Zeitlin
|
1998-12-02 17:32:41 -05:00
|
|
|
// Modified by:
|
1998-08-03 18:37:42 -04:00
|
|
|
// Created: 03.08.98
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
1998-08-03 18:37:42 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
2009-06-01 16:54:03 -04:00
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif //precompiled headers
|
|
|
|
|
|
|
|
#include "wx/log.h"
|
|
|
|
#include "wx/config.h"
|
|
|
|
|
2012-03-03 19:28:58 -05:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2009-02-07 20:01:00 -05:00
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// classes
|
|
|
|
// ----------------------------------------------------------------------------
|
2001-07-23 13:46:15 -04:00
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
class MyApp: public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// implement base class virtuals
|
2014-03-29 20:02:23 -04:00
|
|
|
virtual bool OnInit() wxOVERRIDE;
|
|
|
|
virtual int OnExit() wxOVERRIDE;
|
1998-08-03 18:37:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
2009-02-07 20:01:00 -05:00
|
|
|
MyFrame();
|
|
|
|
virtual ~MyFrame();
|
1998-12-02 17:32:41 -05:00
|
|
|
|
2009-02-07 20:01:00 -05:00
|
|
|
// callbacks
|
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
void OnDelete(wxCommandEvent& event);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
private:
|
2009-02-07 20:01:00 -05:00
|
|
|
wxTextCtrl *m_text;
|
|
|
|
wxCheckBox *m_check;
|
1998-08-03 18:37:42 -04:00
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxDECLARE_EVENT_TABLE();
|
1998-08-03 18:37:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// event tables
|
|
|
|
// ----------------------------------------------------------------------------
|
2001-07-23 13:46:15 -04:00
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2010-12-22 08:57:08 -05:00
|
|
|
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
|
|
|
|
EVT_MENU(wxID_DELETE, MyFrame::OnDelete)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// application
|
|
|
|
// ----------------------------------------------------------------------------
|
2001-07-23 13:46:15 -04:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2009-02-07 20:01:00 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// we're using wxConfig's "create-on-demand" feature: it will create the
|
|
|
|
// config object when it's used for the first time. It has a number of
|
|
|
|
// advantages compared with explicitly creating our wxConfig:
|
|
|
|
// 1) we don't pay for it if we don't use it
|
|
|
|
// 2) there is no danger to create it twice
|
|
|
|
|
|
|
|
// application and vendor name are used by wxConfig to construct the name
|
|
|
|
// of the config file/registry key and must be set before the first call
|
|
|
|
// to Get() if you want to override the default values (the application
|
|
|
|
// name is the name of the executable and the vendor name is the same)
|
|
|
|
SetVendorName("wxWidgets");
|
|
|
|
SetAppName("conftest"); // not needed, it's the default value
|
|
|
|
|
|
|
|
wxConfigBase *pConfig = wxConfigBase::Get();
|
1998-08-03 18:37:42 -04:00
|
|
|
|
2009-02-07 20:01:00 -05:00
|
|
|
// uncomment this to force writing back of the defaults for all values
|
|
|
|
// if they're not present in the config - this can give the user an idea
|
|
|
|
// of all possible settings for this program
|
|
|
|
pConfig->SetRecordDefaults();
|
|
|
|
|
|
|
|
// or you could also write something like this:
|
|
|
|
// wxFileConfig *pConfig = new wxFileConfig("conftest");
|
|
|
|
// wxConfigBase::Set(pConfig);
|
|
|
|
// where you can also specify the file names explicitly if you wish.
|
|
|
|
// Of course, calling Set() is optional and you only must do it if
|
|
|
|
// you want to later retrieve this pointer with Get().
|
|
|
|
|
|
|
|
// create the main program window
|
|
|
|
MyFrame *frame = new MyFrame;
|
|
|
|
frame->Show(true);
|
|
|
|
|
|
|
|
// use our config object...
|
|
|
|
if ( pConfig->Read("/Controls/Check", 1l) != 0 ) {
|
|
|
|
wxMessageBox("You can disable this message box by unchecking\n"
|
|
|
|
"the checkbox in the main window (of course, a real\n"
|
|
|
|
"program would have a checkbox right here but we\n"
|
|
|
|
"keep it simple)", "Welcome to wxConfig demo",
|
|
|
|
wxICON_INFORMATION | wxOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
1998-08-03 18:37:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
2009-02-07 20:01:00 -05:00
|
|
|
// clean up: Set() returns the active config object as Get() does, but unlike
|
|
|
|
// Get() it doesn't try to create one if there is none (definitely not what
|
|
|
|
// we want here!)
|
|
|
|
delete wxConfigBase::Set((wxConfigBase *) NULL);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
2009-02-07 20:01:00 -05:00
|
|
|
return 0;
|
1998-08-03 18:37:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// main frame ctor
|
|
|
|
MyFrame::MyFrame()
|
2004-05-27 13:50:54 -04:00
|
|
|
: wxFrame((wxFrame *) NULL, wxID_ANY, "wxConfig Demo")
|
1998-08-03 18:37:42 -04:00
|
|
|
{
|
2009-02-07 20:01:00 -05:00
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
// menu
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
2010-12-22 08:57:08 -05:00
|
|
|
file_menu->Append(wxID_DELETE, "&Delete", "Delete config file");
|
2009-02-07 20:01:00 -05:00
|
|
|
file_menu->AppendSeparator();
|
2010-12-22 08:57:08 -05:00
|
|
|
file_menu->Append(wxID_ABOUT, "&About\tF1", "About this sample");
|
2009-02-07 20:01:00 -05:00
|
|
|
file_menu->AppendSeparator();
|
2010-12-22 08:57:08 -05:00
|
|
|
file_menu->Append(wxID_EXIT, "E&xit\tAlt-X", "Exit the program");
|
2009-02-07 20:01:00 -05:00
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
|
|
|
SetMenuBar(menu_bar);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
2004-07-19 11:36:01 -04:00
|
|
|
#if wxUSE_STATUSBAR
|
2009-02-07 20:01:00 -05:00
|
|
|
CreateStatusBar();
|
2004-07-19 11:36:01 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
1998-08-03 18:37:42 -04:00
|
|
|
|
2009-02-07 20:01:00 -05:00
|
|
|
// child controls
|
|
|
|
wxPanel *panel = new wxPanel(this);
|
2015-06-17 04:59:27 -04:00
|
|
|
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");
|
|
|
|
|
2015-06-17 09:16:52 -04:00
|
|
|
// put everything in a sizer
|
2015-06-17 04:59:27 -04:00
|
|
|
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);
|
2009-02-07 20:01:00 -05:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// SetPath() understands ".."
|
|
|
|
pConfig->SetPath("../MainFrame");
|
|
|
|
|
|
|
|
// restore frame position and size
|
|
|
|
int x = pConfig->Read("x", 50),
|
|
|
|
y = pConfig->Read("y", 50),
|
|
|
|
w = pConfig->Read("w", 350),
|
|
|
|
h = pConfig->Read("h", 200);
|
|
|
|
Move(x, y);
|
|
|
|
SetClientSize(w, h);
|
|
|
|
|
|
|
|
pConfig->SetPath("/");
|
|
|
|
wxString s;
|
|
|
|
if ( pConfig->Read("TestValue", &s) )
|
|
|
|
{
|
2018-11-01 19:32:02 -04:00
|
|
|
wxLogStatus(this, "TestValue from config is '%s'", s);
|
2009-02-07 20:01:00 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogStatus(this, "TestValue not found in the config");
|
|
|
|
}
|
1999-02-05 18:55:04 -05:00
|
|
|
}
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent&)
|
|
|
|
{
|
2009-02-07 20:01:00 -05:00
|
|
|
Close(true);
|
1998-08-03 18:37:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent&)
|
|
|
|
{
|
2009-02-07 20:01:00 -05:00
|
|
|
wxMessageBox("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin", "About",
|
|
|
|
wxICON_INFORMATION | wxOK);
|
1998-08-03 18:37:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnDelete(wxCommandEvent&)
|
|
|
|
{
|
2002-09-04 09:50:58 -04:00
|
|
|
wxConfigBase *pConfig = wxConfigBase::Get();
|
|
|
|
if ( pConfig == NULL )
|
|
|
|
{
|
|
|
|
wxLogError("No config to delete!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pConfig->DeleteAll() )
|
2001-07-23 13:46:15 -04:00
|
|
|
{
|
1999-07-25 07:44:08 -04:00
|
|
|
wxLogMessage("Config file/registry key successfully deleted.");
|
1999-01-23 18:50:24 -05:00
|
|
|
|
2002-09-04 09:50:58 -04:00
|
|
|
delete wxConfigBase::Set(NULL);
|
1999-01-23 18:50:24 -05:00
|
|
|
wxConfigBase::DontCreateOnDemand();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-07-25 07:44:08 -04:00
|
|
|
wxLogError("Deleting config file/registry key failed.");
|
1999-01-23 18:50:24 -05:00
|
|
|
}
|
1998-08-03 18:37:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
2009-02-07 20:01:00 -05:00
|
|
|
wxConfigBase *pConfig = wxConfigBase::Get();
|
|
|
|
if ( pConfig == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// save the control's values to the config
|
|
|
|
pConfig->Write("/Controls/Text", m_text->GetValue());
|
|
|
|
pConfig->Write("/Controls/Check", m_check->GetValue());
|
|
|
|
|
|
|
|
// save the frame position
|
|
|
|
int x, y, w, h;
|
|
|
|
GetClientSize(&w, &h);
|
|
|
|
GetPosition(&x, &y);
|
|
|
|
pConfig->Write("/MainFrame/x", (long) x);
|
|
|
|
pConfig->Write("/MainFrame/y", (long) y);
|
|
|
|
pConfig->Write("/MainFrame/w", (long) w);
|
|
|
|
pConfig->Write("/MainFrame/h", (long) h);
|
|
|
|
|
|
|
|
pConfig->Write("/TestValue", "A test value");
|
1998-08-03 18:37:42 -04:00
|
|
|
}
|
2002-09-04 09:50:58 -04:00
|
|
|
|