]>
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 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 |
32 | class MyApp: public wxApp |
33 | { | |
34 | public: | |
35 | // implement base class virtuals | |
36 | virtual bool OnInit(); | |
37 | virtual int OnExit(); | |
38 | }; | |
39 | ||
40 | class MyFrame: public wxFrame | |
41 | { | |
42 | public: | |
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 | |
51 | private: | |
52 | wxTextCtrl *m_text; | |
53 | wxCheckBox *m_check; | |
54 | ||
55 | DECLARE_EVENT_TABLE() | |
56 | }; | |
57 | ||
58 | enum | |
59 | { | |
aba4387c VZ |
60 | ConfTest_Quit, |
61 | ConfTest_About, | |
62 | ConfTest_Delete | |
0be4095a VZ |
63 | }; |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // event tables | |
67 | // ---------------------------------------------------------------------------- | |
aba4387c | 68 | |
0be4095a | 69 | BEGIN_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 |
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 | { | |
88 | // we're using wxConfig's "create-on-demand" feature: it will create the | |
89 | // config object when it's used for the first time. It has a number of | |
90 | // advantages compared with explicitly creating our wxConfig: | |
91 | // 1) we don't pay for it if we don't use it | |
92 | // 2) there is no danger to create it twice | |
93 | ||
94 | // application and vendor name are used by wxConfig to construct the name | |
95 | // of the config file/registry key and must be set before the first call | |
96 | // to Get() if you want to override the default values (the application | |
97 | // name is the name of the executable and the vendor name is the same) | |
be5a51fb | 98 | SetVendorName(_T("wxWidgets")); |
9f84eccd | 99 | SetAppName(_T("conftest")); // not needed, it's the default value |
0be4095a VZ |
100 | |
101 | wxConfigBase *pConfig = wxConfigBase::Get(); | |
102 | ||
2ba41305 VZ |
103 | // uncomment this to force writing back of the defaults for all values |
104 | // if they're not present in the config - this can give the user an idea | |
105 | // of all possible settings for this program | |
106 | pConfig->SetRecordDefaults(); | |
107 | ||
0be4095a VZ |
108 | // or you could also write something like this: |
109 | // wxFileConfig *pConfig = new wxFileConfig("conftest"); | |
110 | // wxConfigBase::Set(pConfig); | |
111 | // where you can also specify the file names explicitly if you wish. | |
112 | // Of course, calling Set() is optional and you only must do it if | |
113 | // you want to later retrieve this pointer with Get(). | |
114 | ||
115 | // create the main program window | |
116 | MyFrame *frame = new MyFrame; | |
5014bb3a | 117 | frame->Show(true); |
0be4095a VZ |
118 | SetTopWindow(frame); |
119 | ||
120 | // use our config object... | |
9f84eccd MB |
121 | if ( pConfig->Read(_T("/Controls/Check"), 1l) != 0 ) { |
122 | wxMessageBox(_T("You can disable this message box by unchecking\n") | |
123 | _T("the checkbox in the main window (of course, a real\n") | |
124 | _T("program would have a checkbox right here but we\n") | |
125 | _T("keep it simple)"), _T("Welcome to wxConfig demo"), | |
0be4095a VZ |
126 | wxICON_INFORMATION | wxOK); |
127 | } | |
128 | ||
5014bb3a | 129 | return true; |
0be4095a VZ |
130 | } |
131 | ||
132 | int MyApp::OnExit() | |
133 | { | |
134 | // clean up: Set() returns the active config object as Get() does, but unlike | |
135 | // Get() it doesn't try to create one if there is none (definitely not what | |
136 | // we want here!) | |
c67daf87 | 137 | delete wxConfigBase::Set((wxConfigBase *) NULL); |
0be4095a VZ |
138 | |
139 | return 0; | |
140 | } | |
141 | ||
142 | // ---------------------------------------------------------------------------- | |
143 | // frame | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
146 | // main frame ctor | |
147 | MyFrame::MyFrame() | |
5014bb3a | 148 | : wxFrame((wxFrame *) NULL, wxID_ANY, _T("wxConfig Demo")) |
0be4095a VZ |
149 | { |
150 | // menu | |
151 | wxMenu *file_menu = new wxMenu; | |
152 | ||
9f84eccd | 153 | file_menu->Append(ConfTest_Delete, _T("&Delete"), _T("Delete config file")); |
0be4095a | 154 | file_menu->AppendSeparator(); |
9f84eccd | 155 | file_menu->Append(ConfTest_About, _T("&About\tF1"), _T("About this sample")); |
0be4095a | 156 | file_menu->AppendSeparator(); |
9f84eccd | 157 | file_menu->Append(ConfTest_Quit, _T("E&xit\tAlt-X"), _T("Exit the program")); |
0be4095a | 158 | wxMenuBar *menu_bar = new wxMenuBar; |
9f84eccd | 159 | menu_bar->Append(file_menu, _T("&File")); |
0be4095a VZ |
160 | SetMenuBar(menu_bar); |
161 | ||
8520f137 | 162 | #if wxUSE_STATUSBAR |
0be4095a | 163 | CreateStatusBar(); |
8520f137 | 164 | #endif // wxUSE_STATUSBAR |
0be4095a VZ |
165 | |
166 | // child controls | |
167 | wxPanel *panel = new wxPanel(this); | |
5014bb3a | 168 | (void)new wxStaticText(panel, wxID_ANY, _T("These controls remember their values!"), |
0be4095a | 169 | wxPoint(10, 10), wxSize(300, 20)); |
5014bb3a WS |
170 | m_text = new wxTextCtrl(panel, wxID_ANY, _T(""), wxPoint(10, 40), wxSize(300, 20)); |
171 | m_check = new wxCheckBox(panel, wxID_ANY, _T("show welcome message box at startup"), | |
0be4095a VZ |
172 | wxPoint(10, 70), wxSize(300, 20)); |
173 | ||
174 | // restore the control's values from the config | |
175 | ||
176 | // NB: in this program, the config object is already created at this moment | |
177 | // because we had called Get() from MyApp::OnInit(). However, if you later | |
178 | // change the code and don't create it before this line, it won't break | |
179 | // anything - unlike if you manually create wxConfig object with Create() | |
180 | // or in any other way (then you must be sure to create it before using it!). | |
181 | wxConfigBase *pConfig = wxConfigBase::Get(); | |
182 | ||
183 | // we could write Read("/Controls/Text") as well, it's just to show SetPath() | |
9f84eccd | 184 | pConfig->SetPath(_T("/Controls")); |
0be4095a | 185 | |
9f84eccd MB |
186 | m_text->SetValue(pConfig->Read(_T("Text"), _T(""))); |
187 | m_check->SetValue(pConfig->Read(_T("Check"), 1l) != 0); | |
0be4095a VZ |
188 | |
189 | // SetPath() understands ".." | |
9f84eccd | 190 | pConfig->SetPath(_T("../MainFrame")); |
0be4095a VZ |
191 | |
192 | // restore frame position and size | |
9f84eccd MB |
193 | int x = pConfig->Read(_T("x"), 50), |
194 | y = pConfig->Read(_T("y"), 50), | |
195 | w = pConfig->Read(_T("w"), 350), | |
196 | h = pConfig->Read(_T("h"), 200); | |
0be4095a VZ |
197 | Move(x, y); |
198 | SetClientSize(w, h); | |
0be4095a | 199 | |
9f84eccd | 200 | pConfig->SetPath(_T("/")); |
aba4387c | 201 | wxString s; |
9f84eccd | 202 | if ( pConfig->Read(_T("TestValue"), &s) ) |
aba4387c | 203 | { |
4693b20c | 204 | wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str()); |
aba4387c VZ |
205 | } |
206 | else | |
207 | { | |
4693b20c | 208 | wxLogStatus(this, wxT("TestValue not found in the config")); |
aba4387c | 209 | } |
e3065973 JS |
210 | } |
211 | ||
0be4095a VZ |
212 | void MyFrame::OnQuit(wxCommandEvent&) |
213 | { | |
5014bb3a | 214 | Close(true); |
0be4095a VZ |
215 | } |
216 | ||
217 | void MyFrame::OnAbout(wxCommandEvent&) | |
218 | { | |
2ba41305 | 219 |