]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/config/conftest.cpp
add unit test for wxTextCtrl::SetSelection()
[wxWidgets.git] / samples / config / conftest.cpp
... / ...
CommitLineData
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// ----------------------------------------------------------------------------
29// classes
30// ----------------------------------------------------------------------------
31
32class MyApp: public wxApp
33{
34public:
35 // implement base class virtuals
36 virtual bool OnInit();
37 virtual int OnExit();
38};
39
40class MyFrame: public wxFrame
41{
42public:
43 MyFrame();
44 virtual ~MyFrame();
45
46 // callbacks
47 void OnQuit(wxCommandEvent& event);
48 void OnAbout(wxCommandEvent& event);
49 void OnDelete(wxCommandEvent& event);
50
51private:
52 wxTextCtrl *m_text;
53 wxCheckBox *m_check;
54
55 DECLARE_EVENT_TABLE()
56};
57
58enum
59{
60 ConfTest_Quit,
61 ConfTest_About,
62 ConfTest_Delete
63};
64
65// ----------------------------------------------------------------------------
66// event tables
67// ----------------------------------------------------------------------------
68
69BEGIN_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)
73END_EVENT_TABLE()
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// application
81// ----------------------------------------------------------------------------
82
83IMPLEMENT_APP(MyApp)
84
85// `Main program' equivalent, creating windows and returning main app frame
86bool MyApp::OnInit()
87{
88 if ( !wxApp::OnInit() )
89 return false;
90
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
96
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
103
104 wxConfigBase *pConfig = wxConfigBase::Get();
105
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();
110
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().
117
118 // create the main program window
119 MyFrame *frame = new MyFrame;
120 frame->Show(true);
121 SetTopWindow(frame);
122
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);
130 }
131
132 return true;
133}
134
135int MyApp::OnExit()
136{
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
139 // we want here!)
140 delete wxConfigBase::Set((wxConfigBase *) NULL);
141
142 return 0;
143}
144
145// ----------------------------------------------------------------------------
146// frame
147// ----------------------------------------------------------------------------
148
149// main frame ctor
150MyFrame::MyFrame()
151 : wxFrame((wxFrame *) NULL, wxID_ANY, _T("wxConfig Demo"))
152{
153 // menu
154 wxMenu *file_menu = new wxMenu;
155
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);
164
165#if wxUSE_STATUSBAR
166 CreateStatusBar();
167#endif // wxUSE_STATUSBAR
168
169 // child controls
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));
176
177 // restore the control's values from the config
178
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();
185
186 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
187 pConfig->SetPath(_T("/Controls"));
188
189 m_text->SetValue(pConfig->Read(_T("Text"), _T("")));
190 m_check->SetValue(pConfig->Read(_T("Check"), 1l) != 0);
191
192 // SetPath() understands ".."
193 pConfig->SetPath(_T("../MainFrame"));
194
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);
200 Move(x, y);
201 SetClientSize(w, h);
202
203 pConfig->SetPath(_T("/"));
204 wxString s;
205 if ( pConfig->Read(_T("TestValue"), &s) )
206 {
207 wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str());
208 }
209 else
210 {
211 wxLogStatus(this, wxT("TestValue not found in the config"));
212 }
213}
214
215void MyFrame::OnQuit(wxCommandEvent&)
216{
217 Close(true);
218}
219
220void MyFrame::OnAbout(wxCommandEvent&)
221{
222 wxMessageBox(_T("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), _T("About"),
223 wxICON_INFORMATION | wxOK);
224}
225
226void MyFrame::OnDelete(wxCommandEvent&)
227{
228 wxConfigBase *pConfig = wxConfigBase::Get();
229 if ( pConfig == NULL )
230 {
231 wxLogError(_T("No config to delete!"));
232 return;
233 }
234
235 if ( pConfig->DeleteAll() )
236 {
237 wxLogMessage(_T("Config file/registry key successfully deleted."));
238
239 delete wxConfigBase::Set(NULL);
240 wxConfigBase::DontCreateOnDemand();
241 }
242 else
243 {
244 wxLogError(_T("Deleting config file/registry key failed."));
245 }
246}
247
248MyFrame::~MyFrame()
249{
250 wxConfigBase *pConfig = wxConfigBase::Get();
251 if ( pConfig == NULL )
252 return;
253
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());
257
258 // save the frame position
259 int x, y, w, h;
260 GetClientSize(&w, &h);
261 GetPosition(&x, &y);
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);
266
267 pConfig->Write(_T("/TestValue"), wxT("A test value"));
268}
269