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