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