]> git.saurik.com Git - wxWidgets.git/blame - samples/config/conftest.cpp
fixing file paths after renaming
[wxWidgets.git] / samples / config / conftest.cpp
CommitLineData
0be4095a
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: conftest.cpp
3// Purpose: demo of wxConfig and related classes
4// Author: Vadim Zeitlin
b26c0958 5// Modified by:
0be4095a
VZ
6// Created: 03.08.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#include "wx/wxprec.h"
20
21#ifndef WX_PRECOMP
22 #include "wx/wx.h"
23#endif //precompiled headers
24
25#include "wx/log.h"
26#include "wx/config.h"
27
28// ----------------------------------------------------------------------------
29// classes
30// ----------------------------------------------------------------------------
aba4387c 31
0be4095a
VZ
32class MyApp: public wxApp
33{
34public:
35 // implement base class virtuals
36 virtual bool OnInit();
37 virtual int OnExit();
38};
39
40class MyFrame: public wxFrame
41{
42public:
43 MyFrame();
44 virtual ~MyFrame();
b26c0958 45
0be4095a
VZ
46 // callbacks
47 void OnQuit(wxCommandEvent& event);
48 void OnAbout(wxCommandEvent& event);
49 void OnDelete(wxCommandEvent& event);
0be4095a
VZ
50
51private:
52 wxTextCtrl *m_text;
53 wxCheckBox *m_check;
54
55 DECLARE_EVENT_TABLE()
56};
57
58enum
59{
aba4387c
VZ
60 ConfTest_Quit,
61 ConfTest_About,
62 ConfTest_Delete
0be4095a
VZ
63};
64
65// ----------------------------------------------------------------------------
66// event tables
67// ----------------------------------------------------------------------------
aba4387c 68
0be4095a 69BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aba4387c
VZ
70 EVT_MENU(ConfTest_Quit, MyFrame::OnQuit)
71 EVT_MENU(ConfTest_About, MyFrame::OnAbout)
72 EVT_MENU(ConfTest_Delete, MyFrame::OnDelete)
0be4095a
VZ
73END_EVENT_TABLE()
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// application
81// ----------------------------------------------------------------------------
aba4387c 82
0be4095a
VZ
83IMPLEMENT_APP(MyApp)
84
85// `Main program' equivalent, creating windows and returning main app frame
86bool MyApp::OnInit()
87{
45e6e6f8
VZ
88 if ( !wxApp::OnInit() )
89 return false;
90
0be4095a
VZ
91 // we're using wxConfig's "create-on-demand" feature: it will create the
92 // config object when it's used for the first time. It has a number of
93 // advantages compared with explicitly creating our wxConfig:
94 // 1) we don't pay for it if we don't use it
95 // 2) there is no danger to create it twice
96
97 // application and vendor name are used by wxConfig to construct the name
98 // of the config file/registry key and must be set before the first call
99 // to Get() if you want to override the default values (the application
100 // name is the name of the executable and the vendor name is the same)
be5a51fb 101 SetVendorName(_T("wxWidgets"));
9f84eccd 102 SetAppName(_T("conftest")); // not needed, it's the default value
0be4095a
VZ
103
104 wxConfigBase *pConfig = wxConfigBase::Get();
105
2ba41305
VZ
106 // uncomment this to force writing back of the defaults for all values
107 // if they're not present in the config - this can give the user an idea
108 // of all possible settings for this program
109 pConfig->SetRecordDefaults();
110
0be4095a
VZ
111 // or you could also write something like this:
112 // wxFileConfig *pConfig = new wxFileConfig("conftest");
113 // wxConfigBase::Set(pConfig);
114 // where you can also specify the file names explicitly if you wish.
115 // Of course, calling Set() is optional and you only must do it if
116 // you want to later retrieve this pointer with Get().
117
118 // create the main program window
119 MyFrame *frame = new MyFrame;
5014bb3a 120 frame->Show(true);
0be4095a
VZ
121 SetTopWindow(frame);
122
123 // use our config object...
9f84eccd
MB
124 if ( pConfig->Read(_T("/Controls/Check"), 1l) != 0 ) {
125 wxMessageBox(_T("You can disable this message box by unchecking\n")
126 _T("the checkbox in the main window (of course, a real\n")
127 _T("program would have a checkbox right here but we\n")
128 _T("keep it simple)"), _T("Welcome to wxConfig demo"),
0be4095a
VZ
129 wxICON_INFORMATION | wxOK);
130 }
131
5014bb3a 132 return true;
0be4095a
VZ
133}
134
135int MyApp::OnExit()
136{
137 // clean up: Set() returns the active config object as Get() does, but unlike
138 // Get() it doesn't try to create one if there is none (definitely not what
139 // we want here!)
c67daf87 140 delete wxConfigBase::Set((wxConfigBase *) NULL);
0be4095a
VZ
141
142 return 0;
143}
144
145// ----------------------------------------------------------------------------
146// frame
147// ----------------------------------------------------------------------------
148
149// main frame ctor
150MyFrame::MyFrame()
5014bb3a 151 : wxFrame((wxFrame *) NULL, wxID_ANY, _T("wxConfig Demo"))
0be4095a
VZ
152{
153 // menu
154 wxMenu *file_menu = new wxMenu;
155
9f84eccd 156 file_menu->Append(ConfTest_Delete, _T("&Delete"), _T("Delete config file"));
0be4095a 157 file_menu->AppendSeparator();
9f84eccd 158 file_menu->Append(ConfTest_About, _T("&About\tF1"), _T("About this sample"));
0be4095a 159 file_menu->AppendSeparator();
9f84eccd 160 file_menu->Append(ConfTest_Quit, _T("E&xit\tAlt-X"), _T("Exit the program"));
0be4095a 161 wxMenuBar *menu_bar = new wxMenuBar;
9f84eccd 162 menu_bar->Append(file_menu, _T("&File"));
0be4095a
VZ
163 SetMenuBar(menu_bar);
164
8520f137 165#if wxUSE_STATUSBAR
0be4095a 166 CreateStatusBar();
8520f137 167#endif // wxUSE_STATUSBAR
0be4095a
VZ
168
169 // child controls
170 wxPanel *panel = new wxPanel(this);
5014bb3a 171 (void)new wxStaticText(panel, wxID_ANY, _T("These controls remember their values!"),
0be4095a 172 wxPoint(10, 10), wxSize(300, 20));
5014bb3a
WS
173 m_text = new wxTextCtrl(panel, wxID_ANY, _T(""), wxPoint(10, 40), wxSize(300, 20));
174 m_check = new wxCheckBox(panel, wxID_ANY, _T("show welcome message box at startup"),
0be4095a
VZ
175 wxPoint(10, 70), wxSize(300, 20));
176
177 // restore the control's values from the config
178
179 // NB: in this program, the config object is already created at this moment
180 // because we had called Get() from MyApp::OnInit(). However, if you later
181 // change the code and don't create it before this line, it won't break
182 // anything - unlike if you manually create wxConfig object with Create()
183 // or in any other way (then you must be sure to create it before using it!).
184 wxConfigBase *pConfig = wxConfigBase::Get();
185
186 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
9f84eccd 187 pConfig->SetPath(_T("/Controls"));
0be4095a 188
9f84eccd
MB
189 m_text->SetValue(pConfig->Read(_T("Text"), _T("")));
190 m_check->SetValue(pConfig->Read(_T("Check"), 1l) != 0);
0be4095a
VZ
191
192 // SetPath() understands ".."
9f84eccd 193 pConfig->SetPath(_T("../MainFrame"));
0be4095a
VZ
194
195 // restore frame position and size
9f84eccd
MB
196 int x = pConfig->Read(_T("x"), 50),
197 y = pConfig->Read(_T("y"), 50),
198 w = pConfig->Read(_T("w"), 350),
199 h = pConfig->Read(_T("h"), 200);
0be4095a
VZ
200 Move(x, y);
201 SetClientSize(w, h);
0be4095a 202
9f84eccd 203 pConfig->SetPath(_T("/"));
aba4387c 204 wxString s;
9f84eccd 205 if ( pConfig->Read(_T("TestValue"), &s) )
aba4387c 206 {
4693b20c 207 wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str());
aba4387c
VZ
208 }
209 else
210 {
4693b20c 211 wxLogStatus(this, wxT("TestValue not found in the config"));
aba4387c 212 }
e3065973
JS
213}
214
0be4095a
VZ
215void MyFrame::OnQuit(wxCommandEvent&)
216{
5014bb3a 217 Close(true);
0be4095a
VZ
218}
219
220void MyFrame::OnAbout(wxCommandEvent&)
221{
749bfe9a 222 wxMessageBox(_T("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), _T("About"),
0be4095a
VZ
223 wxICON_INFORMATION | wxOK);
224}
225
226void MyFrame::OnDelete(wxCommandEvent&)
227{
79826591
VZ
228 wxConfigBase *pConfig = wxConfigBase::Get();
229 if ( pConfig == NULL )
230 {
231 wxLogError(_T("No config to delete!"));
232 return;
233 }
234
235 if ( pConfig->DeleteAll() )
aba4387c 236 {
3001340c 237 wxLogMessage(_T("Config file/registry key successfully deleted."));
90186e52 238
79826591 239 delete wxConfigBase::Set(NULL);
90186e52
VZ
240 wxConfigBase::DontCreateOnDemand();
241 }
242 else
243 {
3001340c 244 wxLogError(_T("Deleting config file/registry key failed."));
90186e52 245 }
0be4095a
VZ
246}
247
248MyFrame::~MyFrame()
249{
0be4095a
VZ
250 wxConfigBase *pConfig = wxConfigBase::Get();
251 if ( pConfig == NULL )
252 return;
79826591
VZ
253
254 // save the control's values to the config
9f84eccd
MB
255 pConfig->Write(_T("/Controls/Text"), m_text->GetValue());
256 pConfig->Write(_T("/Controls/Check"), m_check->GetValue());
0be4095a
VZ
257
258 // save the frame position
259 int x, y, w, h;
260 GetClientSize(&w, &h);
261 GetPosition(&x, &y);
9f84eccd
MB
262 pConfig->Write(_T("/MainFrame/x"), (long) x);
263 pConfig->Write(_T("/MainFrame/y"), (long) y);
264 pConfig->Write(_T("/MainFrame/w"), (long) w);
265 pConfig->Write(_T("/MainFrame/h"), (long) h);
aba4387c 266
9f84eccd 267 pConfig->Write(_T("/TestValue"), wxT("A test value"));
0be4095a 268}
79826591 269