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