]>
git.saurik.com Git - wxWidgets.git/blob - samples/config/conftest.cpp
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"
27 #endif //precompiled headers
30 #include "wx/config.h"
33 #include "../sample.xpm"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class MyApp
: public wxApp
43 // implement base class virtuals
44 virtual bool OnInit();
48 class MyFrame
: public wxFrame
55 void OnQuit(wxCommandEvent
& event
);
56 void OnAbout(wxCommandEvent
& event
);
57 void OnDelete(wxCommandEvent
& event
);
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
78 EVT_MENU(ConfTest_Quit
, MyFrame::OnQuit
)
79 EVT_MENU(ConfTest_About
, MyFrame::OnAbout
)
80 EVT_MENU(ConfTest_Delete
, MyFrame::OnDelete
)
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
93 // `Main program' equivalent, creating windows and returning main app frame
96 if ( !wxApp::OnInit() )
99 // we're using wxConfig's "create-on-demand" feature: it will create the
100 // config object when it's used for the first time. It has a number of
101 // advantages compared with explicitly creating our wxConfig:
102 // 1) we don't pay for it if we don't use it
103 // 2) there is no danger to create it twice
105 // application and vendor name are used by wxConfig to construct the name
106 // of the config file/registry key and must be set before the first call
107 // to Get() if you want to override the default values (the application
108 // name is the name of the executable and the vendor name is the same)
109 SetVendorName(_T("wxWidgets"));
110 SetAppName(_T("conftest")); // not needed, it's the default value
112 wxConfigBase
*pConfig
= wxConfigBase::Get();
114 // uncomment this to force writing back of the defaults for all values
115 // if they're not present in the config - this can give the user an idea
116 // of all possible settings for this program
117 pConfig
->SetRecordDefaults();
119 // or you could also write something like this:
120 // wxFileConfig *pConfig = new wxFileConfig("conftest");
121 // wxConfigBase::Set(pConfig);
122 // where you can also specify the file names explicitly if you wish.
123 // Of course, calling Set() is optional and you only must do it if
124 // you want to later retrieve this pointer with Get().
126 // create the main program window
127 MyFrame
*frame
= new MyFrame
;
131 // use our config object...
132 if ( pConfig
->Read(_T("/Controls/Check"), 1l) != 0 ) {
133 wxMessageBox(_T("You can disable this message box by unchecking\n")
134 _T("the checkbox in the main window (of course, a real\n")
135 _T("program would have a checkbox right here but we\n")
136 _T("keep it simple)"), _T("Welcome to wxConfig demo"),
137 wxICON_INFORMATION
| wxOK
);
145 // clean up: Set() returns the active config object as Get() does, but unlike
146 // Get() it doesn't try to create one if there is none (definitely not what
148 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
159 : wxFrame((wxFrame
*) NULL
, wxID_ANY
, _T("wxConfig Demo"))
161 SetIcon(wxICON(sample
));
164 wxMenu
*file_menu
= new wxMenu
;
166 file_menu
->Append(ConfTest_Delete
, _T("&Delete"), _T("Delete config file"));
167 file_menu
->AppendSeparator();
168 file_menu
->Append(ConfTest_About
, _T("&About\tF1"), _T("About this sample"));
169 file_menu
->AppendSeparator();
170 file_menu
->Append(ConfTest_Quit
, _T("E&xit\tAlt-X"), _T("Exit the program"));
171 wxMenuBar
*menu_bar
= new wxMenuBar
;
172 menu_bar
->Append(file_menu
, _T("&File"));
173 SetMenuBar(menu_bar
);
177 #endif // wxUSE_STATUSBAR
180 wxPanel
*panel
= new wxPanel(this);
181 (void)new wxStaticText(panel
, wxID_ANY
, _T("These controls remember their values!"),
182 wxPoint(10, 10), wxSize(300, 20));
183 m_text
= new wxTextCtrl(panel
, wxID_ANY
, _T(""), wxPoint(10, 40), wxSize(300, 20));
184 m_check
= new wxCheckBox(panel
, wxID_ANY
, _T("show welcome message box at startup"),
185 wxPoint(10, 70), wxSize(300, 20));
187 // restore the control's values from the config
189 // NB: in this program, the config object is already created at this moment
190 // because we had called Get() from MyApp::OnInit(). However, if you later
191 // change the code and don't create it before this line, it won't break
192 // anything - unlike if you manually create wxConfig object with Create()
193 // or in any other way (then you must be sure to create it before using it!).
194 wxConfigBase
*pConfig
= wxConfigBase::Get();
196 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
197 pConfig
->SetPath(_T("/Controls"));
199 m_text
->SetValue(pConfig
->Read(_T("Text"), _T("")));
200 m_check
->SetValue(pConfig
->Read(_T("Check"), 1l) != 0);
202 // SetPath() understands ".."
203 pConfig
->SetPath(_T("../MainFrame"));
205 // restore frame position and size
206 int x
= pConfig
->Read(_T("x"), 50),
207 y
= pConfig
->Read(_T("y"), 50),
208 w
= pConfig
->Read(_T("w"), 350),
209 h
= pConfig
->Read(_T("h"), 200);
213 pConfig
->SetPath(_T("/"));
215 if ( pConfig
->Read(_T("TestValue"), &s
) )
217 wxLogStatus(this, wxT("TestValue from config is '%s'"), s
.c_str());
221 wxLogStatus(this, wxT("TestValue not found in the config"));
225 void MyFrame::OnQuit(wxCommandEvent
&)
230 void MyFrame::OnAbout(wxCommandEvent
&)
232 wxMessageBox(_T("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), _T("About"),
233 wxICON_INFORMATION
| wxOK
);
236 void MyFrame::OnDelete(wxCommandEvent
&)
238 wxConfigBase
*pConfig
= wxConfigBase::Get();
239 if ( pConfig
== NULL
)
241 wxLogError(_T("No config to delete!"));
245 if ( pConfig
->DeleteAll() )
247 wxLogMessage(_T("Config file/registry key successfully deleted."));
249 delete wxConfigBase::Set(NULL
);
250 wxConfigBase::DontCreateOnDemand();
254 wxLogError(_T("Deleting config file/registry key failed."));
260 wxConfigBase
*pConfig
= wxConfigBase::Get();
261 if ( pConfig
== NULL
)
264 // save the control's values to the config
265 pConfig
->Write(_T("/Controls/Text"), m_text
->GetValue());
266 pConfig
->Write(_T("/Controls/Check"), m_check
->GetValue());
268 // save the frame position
270 GetClientSize(&w
, &h
);
272 pConfig
->Write(_T("/MainFrame/x"), (long) x
);
273 pConfig
->Write(_T("/MainFrame/y"), (long) y
);
274 pConfig
->Write(_T("/MainFrame/w"), (long) w
);
275 pConfig
->Write(_T("/MainFrame/h"), (long) h
);
277 pConfig
->Write(_T("/TestValue"), wxT("A test value"));