]> git.saurik.com Git - wxWidgets.git/blame - samples/config/conftest.cpp
fixing searchctrl on osx_cocoa, changing type for peer to wxSearchCtrl
[wxWidgets.git] / samples / config / conftest.cpp
CommitLineData
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>
526954c5 9// Licence: wxWindows licence
0be4095a
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#include "wx/wxprec.h"
20
b4715d08
VZ
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif
24
0be4095a
VZ
25#ifndef WX_PRECOMP
26 #include "wx/wx.h"
27#endif //precompiled headers
28
29#include "wx/log.h"
30#include "wx/config.h"
31
41f02b9a
FM
32#ifndef __WXMSW__
33 #include "../sample.xpm"
34#endif
35
0be4095a
VZ
36// ----------------------------------------------------------------------------
37// classes
38// ----------------------------------------------------------------------------
aba4387c 39
0be4095a
VZ
40class MyApp: public wxApp
41{
42public:
43 // implement base class virtuals
44 virtual bool OnInit();
45 virtual int OnExit();
46};
47
48class MyFrame: public wxFrame
49{
50public:
41f02b9a
FM
51 MyFrame();
52 virtual ~MyFrame();
b26c0958 53
41f02b9a
FM
54 // callbacks
55 void OnQuit(wxCommandEvent& event);
56 void OnAbout(wxCommandEvent& event);
57 void OnDelete(wxCommandEvent& event);
0be4095a
VZ
58
59private:
41f02b9a
FM
60 wxTextCtrl *m_text;
61 wxCheckBox *m_check;
0be4095a 62
41f02b9a 63 DECLARE_EVENT_TABLE()
0be4095a
VZ
64};
65
0be4095a
VZ
66// ----------------------------------------------------------------------------
67// event tables
68// ----------------------------------------------------------------------------
aba4387c 69
0be4095a 70BEGIN_EVENT_TABLE(MyFrame, wxFrame)
41271da4
VZ
71 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
72 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
73 EVT_MENU(wxID_DELETE, MyFrame::OnDelete)
0be4095a
VZ
74END_EVENT_TABLE()
75
76// ============================================================================
77// implementation
78// ============================================================================
79
80// ----------------------------------------------------------------------------
81// application
82// ----------------------------------------------------------------------------
aba4387c 83
0be4095a
VZ
84IMPLEMENT_APP(MyApp)
85
86// `Main program' equivalent, creating windows and returning main app frame
87bool MyApp::OnInit()
88{
41f02b9a
FM
89 if ( !wxApp::OnInit() )
90 return false;
91
92 // we're using wxConfig's "create-on-demand" feature: it will create the
93 // config object when it's used for the first time. It has a number of
94 // advantages compared with explicitly creating our wxConfig:
95 // 1) we don't pay for it if we don't use it
96 // 2) there is no danger to create it twice
97
98 // application and vendor name are used by wxConfig to construct the name
99 // of the config file/registry key and must be set before the first call
100 // to Get() if you want to override the default values (the application
101 // name is the name of the executable and the vendor name is the same)
9a83f860
VZ
102 SetVendorName(wxT("wxWidgets"));
103 SetAppName(wxT("conftest")); // not needed, it's the default value
41f02b9a
FM
104
105 wxConfigBase *pConfig = wxConfigBase::Get();
0be4095a 106
41f02b9a
FM
107 // uncomment this to force writing back of the defaults for all values
108 // if they're not present in the config - this can give the user an idea
109 // of all possible settings for this program
110 pConfig->SetRecordDefaults();
111
112 // or you could also write something like this:
113 // wxFileConfig *pConfig = new wxFileConfig("conftest");
114 // wxConfigBase::Set(pConfig);
115 // where you can also specify the file names explicitly if you wish.
116 // Of course, calling Set() is optional and you only must do it if
117 // you want to later retrieve this pointer with Get().
118
119 // create the main program window
120 MyFrame *frame = new MyFrame;
121 frame->Show(true);
41f02b9a
FM
122
123 // use our config object...
9a83f860
VZ
124 if ( pConfig->Read(wxT("/Controls/Check"), 1l) != 0 ) {
125 wxMessageBox(wxT("You can disable this message box by unchecking\n")
126 wxT("the checkbox in the main window (of course, a real\n")
127 wxT("program would have a checkbox right here but we\n")
128 wxT("keep it simple)"), wxT("Welcome to wxConfig demo"),
41f02b9a
FM
129 wxICON_INFORMATION | wxOK);
130 }
131
132 return true;
0be4095a
VZ
133}
134
135int MyApp::OnExit()
136{
41f02b9a
FM
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);
0be4095a 141
41f02b9a 142 return 0;
0be4095a
VZ
143}
144
145// ----------------------------------------------------------------------------
146// frame
147// ----------------------------------------------------------------------------
148
149// main frame ctor
150MyFrame::MyFrame()
9a83f860 151 : wxFrame((wxFrame *) NULL, wxID_ANY, wxT("wxConfig Demo"))
0be4095a 152{
41f02b9a
FM
153 SetIcon(wxICON(sample));
154
155 // menu
156 wxMenu *file_menu = new wxMenu;
157
41271da4 158 file_menu->Append(wxID_DELETE, wxT("&Delete"), wxT("Delete config file"));
41f02b9a 159 file_menu->AppendSeparator();
41271da4 160 file_menu->Append(wxID_ABOUT, wxT("&About\tF1"), wxT("About this sample"));
41f02b9a 161 file_menu->AppendSeparator();
41271da4 162 file_menu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Exit the program"));
41f02b9a 163 wxMenuBar *menu_bar = new wxMenuBar;
9a83f860 164 menu_bar->Append(file_menu, wxT("&File"));
41f02b9a 165 SetMenuBar(menu_bar);
0be4095a 166
8520f137 167#if wxUSE_STATUSBAR
41f02b9a 168 CreateStatusBar();
8520f137 169#endif // wxUSE_STATUSBAR
0be4095a 170
41f02b9a
FM
171 // child controls
172 wxPanel *panel = new wxPanel(this);
9a83f860 173 (void)new wxStaticText(panel, wxID_ANY, wxT("These controls remember their values!"),
41f02b9a 174 wxPoint(10, 10), wxSize(300, 20));
9a83f860
VZ
175 m_text = new wxTextCtrl(panel, wxID_ANY, wxT(""), wxPoint(10, 40), wxSize(300, 20));
176 m_check = new wxCheckBox(panel, wxID_ANY, wxT("show welcome message box at startup"),
41f02b9a
FM
177 wxPoint(10, 70), wxSize(300, 20));
178
179 // restore the control's values from the config
180
181 // NB: in this program, the config object is already created at this moment
182 // because we had called Get() from MyApp::OnInit(). However, if you later
183 // change the code and don't create it before this line, it won't break
184 // anything - unlike if you manually create wxConfig object with Create()
185 // or in any other way (then you must be sure to create it before using it!).
186 wxConfigBase *pConfig = wxConfigBase::Get();
187
188 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
9a83f860 189 pConfig->SetPath(wxT("/Controls"));
41f02b9a 190
9a83f860
VZ
191 m_text->SetValue(pConfig->Read(wxT("Text"), wxT("")));
192 m_check->SetValue(pConfig->Read(wxT("Check"), 1l) != 0);
41f02b9a
FM
193
194 // SetPath() understands ".."
9a83f860 195 pConfig->SetPath(wxT("../MainFrame"));
41f02b9a
FM
196
197 // restore frame position and size
9a83f860
VZ
198 int x = pConfig->Read(wxT("x"), 50),
199 y = pConfig->Read(wxT("y"), 50),
200 w = pConfig->Read(wxT("w"), 350),
201 h = pConfig->Read(wxT("h"), 200);
41f02b9a
FM
202 Move(x, y);
203 SetClientSize(w, h);
204
9a83f860 205 pConfig->SetPath(wxT("/"));
41f02b9a 206 wxString s;
9a83f860 207 if ( pConfig->Read(wxT("TestValue"), &s) )
41f02b9a
FM
208 {
209 wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str());
210 }
211 else
212 {
213 wxLogStatus(this, wxT("TestValue not found in the config"));
214 }
e3065973
JS
215}
216
0be4095a
VZ
217void MyFrame::OnQuit(wxCommandEvent&)
218{
41f02b9a 219 Close(true);
0be4095a
VZ
220}
221
222void MyFrame::OnAbout(wxCommandEvent&)
223{
9a83f860 224 wxMessageBox(wxT("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), wxT("About"),
41f02b9a 225 wxICON_INFORMATION | wxOK);
0be4095a
VZ
226}
227
228void MyFrame::OnDelete(wxCommandEvent&)
229{
79826591
VZ
230 wxConfigBase *pConfig = wxConfigBase::Get();
231 if ( pConfig == NULL )
232 {
9a83f860 233 wxLogError(wxT("No config to delete!"));
79826591
VZ
234 return;
235 }
236
237 if ( pConfig->DeleteAll() )
aba4387c 238 {
9a83f860 239 wxLogMessage(wxT("Config file/registry key successfully deleted."));
90186e52 240
79826591 241 delete wxConfigBase::Set(NULL);
90186e52
VZ
242 wxConfigBase::DontCreateOnDemand();
243 }
244 else
245 {
9a83f860 246 wxLogError(wxT("Deleting config file/registry key failed."));
90186e52 247 }
0be4095a
VZ
248}
249
250MyFrame::~MyFrame()
251{
41f02b9a
FM
252 wxConfigBase *pConfig = wxConfigBase::Get();
253 if ( pConfig == NULL )
254 return;
255
256 // save the control's values to the config
9a83f860
VZ
257 pConfig->Write(wxT("/Controls/Text"), m_text->GetValue());
258 pConfig->Write(wxT("/Controls/Check"), m_check->GetValue());
41f02b9a
FM
259
260 // save the frame position
261 int x, y, w, h;
262 GetClientSize(&w, &h);
263 GetPosition(&x, &y);
9a83f860
VZ
264 pConfig->Write(wxT("/MainFrame/x"), (long) x);
265 pConfig->Write(wxT("/MainFrame/y"), (long) y);
266 pConfig->Write(wxT("/MainFrame/w"), (long) w);
267 pConfig->Write(wxT("/MainFrame/h"), (long) h);
41f02b9a 268
9a83f860 269 pConfig->Write(wxT("/TestValue"), wxT("A test value"));
0be4095a 270}
79826591 271