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
|
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
|
// Licence: wxWindows license
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// declarations
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// headers
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
|
#include "wx/wx.h"
|
|
|
|
|
#endif //precompiled headers
|
|
|
|
|
|
|
|
|
|
#include "wx/log.h"
|
|
|
|
|
#include "wx/config.h"
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// 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
|
|
|
|
|
virtual bool OnInit();
|
|
|
|
|
virtual int OnExit();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MyFrame();
|
|
|
|
|
virtual ~MyFrame();
|
1998-12-02 17:32:41 -05:00
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
|
// callbacks
|
|
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
void OnDelete(wxCommandEvent& event);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
wxTextCtrl *m_text;
|
|
|
|
|
wxCheckBox *m_check;
|
|
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
2001-07-23 13:46:15 -04:00
|
|
|
|
ConfTest_Quit,
|
|
|
|
|
ConfTest_About,
|
|
|
|
|
ConfTest_Delete
|
1998-08-03 18:37:42 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// event tables
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2001-07-23 13:46:15 -04:00
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2001-07-23 13:46:15 -04:00
|
|
|
|
EVT_MENU(ConfTest_Quit, MyFrame::OnQuit)
|
|
|
|
|
EVT_MENU(ConfTest_About, MyFrame::OnAbout)
|
|
|
|
|
EVT_MENU(ConfTest_Delete, MyFrame::OnDelete)
|
1998-08-03 18:37:42 -04:00
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
// implementation
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// application
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2001-07-23 13:46:15 -04:00
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
|
|
// `Main program' equivalent, creating windows and returning main app frame
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
|
{
|
|
|
|
|
// 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)
|
2002-12-13 16:33:14 -05:00
|
|
|
|
SetVendorName(_T("wxWindows"));
|
|
|
|
|
SetAppName(_T("conftest")); // not needed, it's the default value
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
|
|
wxConfigBase *pConfig = wxConfigBase::Get();
|
|
|
|
|
|
2001-12-13 19:58:59 -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();
|
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
|
// 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);
|
|
|
|
|
SetTopWindow(frame);
|
|
|
|
|
|
|
|
|
|
// use our config object...
|
2002-12-13 16:33:14 -05:00
|
|
|
|
if ( pConfig->Read(_T("/Controls/Check"), 1l) != 0 ) {
|
|
|
|
|
wxMessageBox(_T("You can disable this message box by unchecking\n")
|
|
|
|
|
_T("the checkbox in the main window (of course, a real\n")
|
|
|
|
|
_T("program would have a checkbox right here but we\n")
|
|
|
|
|
_T("keep it simple)"), _T("Welcome to wxConfig demo"),
|
1998-08-03 18:37:42 -04:00
|
|
|
|
wxICON_INFORMATION | wxOK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
|
{
|
|
|
|
|
// 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!)
|
1998-08-22 23:22:56 -04:00
|
|
|
|
delete wxConfigBase::Set((wxConfigBase *) NULL);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// frame
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// main frame ctor
|
|
|
|
|
MyFrame::MyFrame()
|
2002-12-13 16:33:14 -05:00
|
|
|
|
: wxFrame((wxFrame *) NULL, -1, _T("wxConfig Demo"))
|
1998-08-03 18:37:42 -04:00
|
|
|
|
{
|
|
|
|
|
// menu
|
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
|
2002-12-13 16:33:14 -05:00
|
|
|
|
file_menu->Append(ConfTest_Delete, _T("&Delete"), _T("Delete config file"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
file_menu->AppendSeparator();
|
2002-12-13 16:33:14 -05:00
|
|
|
|
file_menu->Append(ConfTest_About, _T("&About\tF1"), _T("About this sample"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
file_menu->AppendSeparator();
|
2002-12-13 16:33:14 -05:00
|
|
|
|
file_menu->Append(ConfTest_Quit, _T("E&xit\tAlt-X"), _T("Exit the program"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
2002-12-13 16:33:14 -05:00
|
|
|
|
menu_bar->Append(file_menu, _T("&File"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
SetMenuBar(menu_bar);
|
|
|
|
|
|
|
|
|
|
CreateStatusBar();
|
|
|
|
|
|
|
|
|
|
// child controls
|
|
|
|
|
wxPanel *panel = new wxPanel(this);
|
2002-12-13 16:33:14 -05:00
|
|
|
|
(void)new wxStaticText(panel, -1, _T("These controls remember their values!"),
|
1998-08-03 18:37:42 -04:00
|
|
|
|
wxPoint(10, 10), wxSize(300, 20));
|
2002-12-13 16:33:14 -05:00
|
|
|
|
m_text = new wxTextCtrl(panel, -1, _T(""), wxPoint(10, 40), wxSize(300, 20));
|
|
|
|
|
m_check = new wxCheckBox(panel, -1, _T("show welcome message box at startup"),
|
1998-08-03 18:37:42 -04:00
|
|
|
|
wxPoint(10, 70), wxSize(300, 20));
|
|
|
|
|
|
|
|
|
|
// 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()
|
2002-12-13 16:33:14 -05:00
|
|
|
|
pConfig->SetPath(_T("/Controls"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
2002-12-13 16:33:14 -05:00
|
|
|
|
m_text->SetValue(pConfig->Read(_T("Text"), _T("")));
|
|
|
|
|
m_check->SetValue(pConfig->Read(_T("Check"), 1l) != 0);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
|
|
// SetPath() understands ".."
|
2002-12-13 16:33:14 -05:00
|
|
|
|
pConfig->SetPath(_T("../MainFrame"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
|
|
// restore frame position and size
|
2002-12-13 16:33:14 -05:00
|
|
|
|
int x = pConfig->Read(_T("x"), 50),
|
|
|
|
|
y = pConfig->Read(_T("y"), 50),
|
|
|
|
|
w = pConfig->Read(_T("w"), 350),
|
|
|
|
|
h = pConfig->Read(_T("h"), 200);
|
1998-08-03 18:37:42 -04:00
|
|
|
|
Move(x, y);
|
|
|
|
|
SetClientSize(w, h);
|
|
|
|
|
|
2002-12-13 16:33:14 -05:00
|
|
|
|
pConfig->SetPath(_T("/"));
|
2001-07-23 13:46:15 -04:00
|
|
|
|
wxString s;
|
2002-12-13 16:33:14 -05:00
|
|
|
|
if ( pConfig->Read(_T("TestValue"), &s) )
|
2001-07-23 13:46:15 -04:00
|
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
|
wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str());
|
2001-07-23 13:46:15 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2001-09-21 16:21:44 -04:00
|
|
|
|
wxLogStatus(this, wxT("TestValue not found in the config"));
|
2001-07-23 13:46:15 -04:00
|
|
|
|
}
|
1999-02-05 18:55:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
1998-08-03 18:37:42 -04:00
|
|
|
|
void MyFrame::OnQuit(wxCommandEvent&)
|
|
|
|
|
{
|
|
|
|
|
Close(TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent&)
|
|
|
|
|
{
|
2001-12-13 19:58:59 -05:00
|
|
|
|
wxMessageBox(_T("wxConfig demo\n<EFBFBD> 1998-2001 Vadim Zeitlin"), _T("About"),
|
1998-08-03 18:37:42 -04:00
|
|
|
|
wxICON_INFORMATION | wxOK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyFrame::OnDelete(wxCommandEvent&)
|
|
|
|
|
{
|
2002-09-04 09:50:58 -04:00
|
|
|
|
wxConfigBase *pConfig = wxConfigBase::Get();
|
|
|
|
|
if ( pConfig == NULL )
|
|
|
|
|
{
|
|
|
|
|
wxLogError(_T("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(_T("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(_T("Deleting config file/registry key failed."));
|
1999-01-23 18:50:24 -05:00
|
|
|
|
}
|
1998-08-03 18:37:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
|
{
|
|
|
|
|
wxConfigBase *pConfig = wxConfigBase::Get();
|
|
|
|
|
if ( pConfig == NULL )
|
|
|
|
|
return;
|
2002-09-04 09:50:58 -04:00
|
|
|
|
|
|
|
|
|
// save the control's values to the config
|
2002-12-13 16:33:14 -05:00
|
|
|
|
pConfig->Write(_T("/Controls/Text"), m_text->GetValue());
|
|
|
|
|
pConfig->Write(_T("/Controls/Check"), m_check->GetValue());
|
1998-08-03 18:37:42 -04:00
|
|
|
|
|
|
|
|
|
// save the frame position
|
|
|
|
|
int x, y, w, h;
|
|
|
|
|
GetClientSize(&w, &h);
|
|
|
|
|
GetPosition(&x, &y);
|
2002-12-13 16:33:14 -05:00
|
|
|
|
pConfig->Write(_T("/MainFrame/x"), (long) x);
|
|
|
|
|
pConfig->Write(_T("/MainFrame/y"), (long) y);
|
|
|
|
|
pConfig->Write(_T("/MainFrame/w"), (long) w);
|
|
|
|
|
pConfig->Write(_T("/MainFrame/h"), (long) h);
|
2001-07-23 13:46:15 -04:00
|
|
|
|
|
2002-12-13 16:33:14 -05:00
|
|
|
|
pConfig->Write(_T("/TestValue"), wxT("A test value"));
|
1998-08-03 18:37:42 -04:00
|
|
|
|
}
|
2002-09-04 09:50:58 -04:00
|
|
|
|
|