]>
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"
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 // 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
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)
98 SetVendorName(_T("wxWidgets"));
99 SetAppName(_T("conftest")); // not needed, it's the default value
101 wxConfigBase
*pConfig
= wxConfigBase::Get();
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();
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().
115 // create the main program window
116 MyFrame
*frame
= new MyFrame
;
120 // use our config object...
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"),
126 wxICON_INFORMATION
| wxOK
);
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
137 delete wxConfigBase::Set((wxConfigBase
*) NULL
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
148 : wxFrame((wxFrame
*) NULL
, wxID_ANY
, _T("wxConfig Demo"))
151 wxMenu
*file_menu
= new wxMenu
;
153 file_menu
->Append(ConfTest_Delete
, _T("&Delete"), _T("Delete config file"));
154 file_menu
->AppendSeparator();
155 file_menu
->Append(ConfTest_About
, _T("&About\tF1"), _T("About this sample"));
156 file_menu
->AppendSeparator();
157 file_menu
->Append(ConfTest_Quit
, _T("E&xit\tAlt-X"), _T("Exit the program"));
158 wxMenuBar
*menu_bar
= new wxMenuBar
;
159 menu_bar
->Append(file_menu
, _T("&File"));
160 SetMenuBar(menu_bar
);
164 #endif // wxUSE_STATUSBAR
167 wxPanel
*panel
= new wxPanel(this);
168 (void)new wxStaticText(panel
, wxID_ANY
, _T("These controls remember their values!"),
169 wxPoint(10, 10), wxSize(300, 20));
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"),
172 wxPoint(10, 70), wxSize(300, 20));
174 // restore the control's values from the config
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();
183 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
184 pConfig
->SetPath(_T("/Controls"));
186 m_text
->SetValue(pConfig
->Read(_T("Text"), _T("")));
187 m_check
->SetValue(pConfig
->Read(_T("Check"), 1l) != 0);
189 // SetPath() understands ".."
190 pConfig
->SetPath(_T("../MainFrame"));
192 // restore frame position and size
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);
200 pConfig
->SetPath(_T("/"));
202 if ( pConfig
->Read(_T("TestValue"), &s
) )
204 wxLogStatus(this, wxT("TestValue from config is '%s'"), s
.c_str());
208 wxLogStatus(this, wxT("TestValue not found in the config"));
212 void MyFrame::OnQuit(wxCommandEvent
&)
217 void MyFrame::OnAbout(wxCommandEvent
&)
219 wxMessageBox(_T("wxConfig demo\n© 1998-2001 Vadim Zeitlin"), _T("About"),
220 wxICON_INFORMATION
| wxOK
);
223 void MyFrame::OnDelete(wxCommandEvent
&)
225 wxConfigBase
*pConfig
= wxConfigBase::Get();
226 if ( pConfig
== NULL
)
228 wxLogError(_T("No config to delete!"));
232 if ( pConfig
->DeleteAll() )
234 wxLogMessage(_T("Config file/registry key successfully deleted."));
236 delete wxConfigBase::Set(NULL
);
237 wxConfigBase::DontCreateOnDemand();
241 wxLogError(_T("Deleting config file/registry key failed."));
247 wxConfigBase
*pConfig
= wxConfigBase::Get();
248 if ( pConfig
== NULL
)
251 // save the control's values to the config
252 pConfig
->Write(_T("/Controls/Text"), m_text
->GetValue());
253 pConfig
->Write(_T("/Controls/Check"), m_check
->GetValue());
255 // save the frame position
257 GetClientSize(&w
, &h
);
259 pConfig
->Write(_T("/MainFrame/x"), (long) x
);
260 pConfig
->Write(_T("/MainFrame/y"), (long) y
);
261 pConfig
->Write(_T("/MainFrame/w"), (long) w
);
262 pConfig
->Write(_T("/MainFrame/h"), (long) h
);
264 pConfig
->Write(_T("/TestValue"), wxT("A test value"));