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