]> git.saurik.com Git - wxWidgets.git/blame - samples/config/conftest.cpp
fix for wxFrame parent which somehow wasn't committed yesterday
[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>
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// ----------------------------------------------------------------------------
aba4387c 31
0be4095a
VZ
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();
b26c0958 45
0be4095a
VZ
46 // callbacks
47 void OnQuit(wxCommandEvent& event);
48 void OnAbout(wxCommandEvent& event);
49 void OnDelete(wxCommandEvent& event);
0be4095a
VZ
50
51private:
52 wxTextCtrl *m_text;
53 wxCheckBox *m_check;
54
55 DECLARE_EVENT_TABLE()
56};
57
58enum
59{
aba4387c
VZ
60 ConfTest_Quit,
61 ConfTest_About,
62 ConfTest_Delete
0be4095a
VZ
63};
64
65// ----------------------------------------------------------------------------
66// event tables
67// ----------------------------------------------------------------------------
aba4387c 68
0be4095a 69BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aba4387c
VZ
70 EVT_MENU(ConfTest_Quit, MyFrame::OnQuit)
71 EVT_MENU(ConfTest_About, MyFrame::OnAbout)
72 EVT_MENU(ConfTest_Delete, MyFrame::OnDelete)
0be4095a
VZ
73END_EVENT_TABLE()
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// application
81// ----------------------------------------------------------------------------
aba4387c 82
0be4095a
VZ
83IMPLEMENT_APP(MyApp)
84
85// `Main program' equivalent, creating windows and returning main app frame
86bool MyApp::OnInit()
87{
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
93
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("wxWindows");
99 SetAppName("conftest"); // not needed, it's the default value
100
101 wxConfigBase *pConfig = wxConfigBase::Get();
102
103 // or you could also write something like this:
104 // wxFileConfig *pConfig = new wxFileConfig("conftest");
105 // wxConfigBase::Set(pConfig);
106 // where you can also specify the file names explicitly if you wish.
107 // Of course, calling Set() is optional and you only must do it if
108 // you want to later retrieve this pointer with Get().
109
110 // create the main program window
111 MyFrame *frame = new MyFrame;
112 frame->Show(TRUE);
113 SetTopWindow(frame);
114
115 // use our config object...
116 if ( pConfig->Read("/Controls/Check", 1l) != 0 ) {
117 wxMessageBox("You can disable this message box by unchecking\n"
118 "the checkbox in the main window (of course, a real\n"
119 "program would have a checkbox right here but we\n"
120 "keep it simple)", "Welcome to wxConfig demo",
121 wxICON_INFORMATION | wxOK);
122 }
123
124 return TRUE;
125}
126
127int MyApp::OnExit()
128{
129 // clean up: Set() returns the active config object as Get() does, but unlike
130 // Get() it doesn't try to create one if there is none (definitely not what
131 // we want here!)
c67daf87 132 delete wxConfigBase::Set((wxConfigBase *) NULL);
0be4095a
VZ
133
134 return 0;
135}
136
137// ----------------------------------------------------------------------------
138// frame
139// ----------------------------------------------------------------------------
140
141// main frame ctor
142MyFrame::MyFrame()
c67daf87 143 : wxFrame((wxFrame *) NULL, -1, "wxConfig Demo")
0be4095a
VZ
144{
145 // menu
146 wxMenu *file_menu = new wxMenu;
147
aba4387c 148 file_menu->Append(ConfTest_Delete, "&Delete", "Delete config file");
0be4095a 149 file_menu->AppendSeparator();
aba4387c 150 file_menu->Append(ConfTest_About, "&About\tF1", "About this sample");
0be4095a 151 file_menu->AppendSeparator();
aba4387c 152 file_menu->Append(ConfTest_Quit, "E&xit\tAlt-X", "Exit the program");
0be4095a
VZ
153 wxMenuBar *menu_bar = new wxMenuBar;
154 menu_bar->Append(file_menu, "&File");
155 SetMenuBar(menu_bar);
156
157 CreateStatusBar();
158
159 // child controls
160 wxPanel *panel = new wxPanel(this);
161 (void)new wxStaticText(panel, -1, "These controls remember their values!",
162 wxPoint(10, 10), wxSize(300, 20));
163 m_text = new wxTextCtrl(panel, -1, "", wxPoint(10, 40), wxSize(300, 20));
164 m_check = new wxCheckBox(panel, -1, "show welcome message box at startup",
165 wxPoint(10, 70), wxSize(300, 20));
166
167 // restore the control's values from the config
168
169 // NB: in this program, the config object is already created at this moment
170 // because we had called Get() from MyApp::OnInit(). However, if you later
171 // change the code and don't create it before this line, it won't break
172 // anything - unlike if you manually create wxConfig object with Create()
173 // or in any other way (then you must be sure to create it before using it!).
174 wxConfigBase *pConfig = wxConfigBase::Get();
175
176 // we could write Read("/Controls/Text") as well, it's just to show SetPath()
177 pConfig->SetPath("/Controls");
178
179 m_text->SetValue(pConfig->Read("Text", ""));
180 m_check->SetValue(pConfig->Read("Check", 1l) != 0);
181
182 // SetPath() understands ".."
183 pConfig->SetPath("../MainFrame");
184
185 // restore frame position and size
186 int x = pConfig->Read("x", 50),
187 y = pConfig->Read("y", 50),
188 w = pConfig->Read("w", 350),
189 h = pConfig->Read("h", 200);
190 Move(x, y);
191 SetClientSize(w, h);
0be4095a 192
aba4387c
VZ
193 pConfig->SetPath("/");
194 wxString s;
195 if ( pConfig->Read("TestValue", &s) )
196 {
4693b20c 197 wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str());
aba4387c
VZ
198 }
199 else
200 {
4693b20c 201 wxLogStatus(this, wxT("TestValue not found in the config"));
aba4387c 202 }
e3065973
JS
203}
204
0be4095a
VZ
205void MyFrame::OnQuit(wxCommandEvent&)
206{
207 Close(TRUE);
208}
209
210void MyFrame::OnAbout(wxCommandEvent&)
211{
3001340c 212