]>
git.saurik.com Git - wxWidgets.git/blob - samples/config/conftest.cpp
aea01ea493ee927efdeb41e2b7ddd1fe1309d5c3
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: demo of wxConfig and related classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
23 #endif //precompiled headers
26 #include "wx/config.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 class MyApp
: public wxApp
35 // implement base class virtuals
36 virtual bool OnInit();
40 class MyFrame
: public wxFrame
47 void OnQuit(wxCommandEvent
& event
);
48 void OnAbout(wxCommandEvent
& event
);
49 void OnDelete(wxCommandEvent
& event
);
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
70 EVT_MENU(ConfTest_Quit
, MyFrame::OnQuit
)
71 EVT_MENU(ConfTest_About
, MyFrame::OnAbout
)
72 EVT_MENU(ConfTest_Delete
, MyFrame::OnDelete
)
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
85 // `Main program' equivalent, creating windows and returning main app frame
88 if ( !wxApp::OnInit() )
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
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)
101 SetVendorName(_T("wxWidgets"));
102 SetAppName(_T("conftest")); // not needed, it's the default value
104 wxConfigBase
*pConfig
= wxConfigBase::Get();
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();
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().
118 // create the main program window
119 MyFrame
*frame
= new MyFrame
;
123 // use our config object...
124 if ( pConfig
->Read(_T("/Controls/Check"), 1l) != 0 ) {
125 wxMessageBox(_T("You can disable this message box by unchecking\n")
126 _T("the checkbox in the main window (of course, a real\n")
127 _T("program would have a checkbox right here but we\n")
128 _T("keep it simple)"), _T("Welcome to wxConfig demo"),
129 wxICON_INFORMATION
| wxOK
);
137 // clean up: Set() returns the active config object as Get() does, but unlike
138 // Get() it doesn't try to create one if there is none (definitely not what
140 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
151 : wxFrame((wxFrame
*) NULL
, wxID_ANY
, _T("wxConfig Demo"))
154 wxMenu
*file_menu
= new wxMenu
;
156 file_menu
->Append(ConfTest_Delete
, _T("&Delete"), _T("Delete config file"));
157 file_menu
->AppendSeparator();
158 file_menu
->Append(ConfTest_About
, _T("&About\tF1"), _T("About this sample"));
159 file_menu
->AppendSeparator();
160 file_menu
->Append(ConfTest_Quit
, _T("E&xit\tAlt-X"), _T("Exit the program"));
161 wxMenuBar
*menu_bar
= new wxMenuBar
;
162 menu_bar
->Append(file_menu
, _T("&File"));
163 SetMenuBar(menu_bar
);
167 #endif // wxUSE_STATUSBAR
170 wxPanel
*panel
= new wxPanel(this);
171 (void)new wxStaticText(panel
, wxID_ANY
, _T("These controls remember their values!"),
172 wxPoint(10, 10), wxSize(300, 20));
173 m_text
= new wxTextCtrl(panel
, wxID_ANY
, _T(""), wxPoint(10, 40), wxSize(300, 20));
174 m_check
= new wxCheckBox(panel
, wxID_ANY
, _T("show welcome message box at startup"),
175 wxPoint(10, 70), wxSize(300, 20));
177 // restore the control's values from the config
179 // NB: in this program, the config object is already created at this moment
180 // because we had called Get() from MyApp::OnInit(). However, if you later
181 // change the code and don't create it before this line, it won't break
182 // anything - unlike if you manually create wxConfig object with Create()
183 // or in any other way (then you must be sure to create it before using it!).
184 wxConfigBase
*pConfig
= wxConfigBase::Get();
186 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
187 pConfig
->SetPath(_T("/Controls"));
189 m_text
->SetValue(pConfig
->Read(_T("Text"), _T("")));
190 m_check
->SetValue(pConfig
->Read(_T("Check"), 1l) != 0);
192 // SetPath() understands ".."
193 pConfig
->SetPath(_T("../MainFrame"));
195 // restore frame position and size
196 int x
= pConfig
->Read(_T("x"), 50),
197 y
= pConfig
->Read(_T("y"), 50),
198 w
= pConfig
->Read(_T("w"), 350),
199 h
= pConfig
->Read(_T("h"), 200);
203 pConfig
->SetPath(_T("/"));
205 if ( pConfig
->Read(_T("TestValue"), &s
) )
207 wxLogStatus(this, wxT("TestValue from config is '%s'"), s
.c_str());
211 wxLogStatus(this, wxT("TestValue not found in the config"));
215 void MyFrame::OnQuit(wxCommandEvent
&)
220 void MyFrame::OnAbout(wxCommandEvent
&)
222 wxMessageBox(_T("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), _T("About"),
223 wxICON_INFORMATION
| wxOK
);
226 void MyFrame::OnDelete(wxCommandEvent
&)
228 wxConfigBase
*pConfig
= wxConfigBase::Get();
229 if ( pConfig
== NULL
)
231 wxLogError(_T("No config to delete!"));
235 if ( pConfig
->DeleteAll() )
237 wxLogMessage(_T("Config file/registry key successfully deleted."));
239 delete wxConfigBase::Set(NULL
);
240 wxConfigBase::DontCreateOnDemand();
244 wxLogError(_T("Deleting config file/registry key failed."));
250 wxConfigBase
*pConfig
= wxConfigBase::Get();
251 if ( pConfig
== NULL
)
254 // save the control's values to the config
255 pConfig
->Write(_T("/Controls/Text"), m_text
->GetValue());
256 pConfig
->Write(_T("/Controls/Check"), m_check
->GetValue());
258 // save the frame position
260 GetClientSize(&w
, &h
);
262 pConfig
->Write(_T("/MainFrame/x"), (long) x
);
263 pConfig
->Write(_T("/MainFrame/y"), (long) y
);
264 pConfig
->Write(_T("/MainFrame/w"), (long) w
);
265 pConfig
->Write(_T("/MainFrame/h"), (long) h
);
267 pConfig
->Write(_T("/TestValue"), wxT("A test value"));