]>
Commit | Line | Data |
---|---|---|
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 | // ---------------------------------------------------------------------------- | |
31 | class MyApp: public wxApp | |
32 | { | |
33 | public: | |
34 | // implement base class virtuals | |
35 | virtual bool OnInit(); | |
36 | virtual int OnExit(); | |
37 | }; | |
38 | ||
39 | class MyFrame: public wxFrame | |
40 | { | |
41 | public: | |
42 | MyFrame(); | |
43 | virtual ~MyFrame(); | |
b26c0958 | 44 | |
0be4095a VZ |
45 | // callbacks |
46 | void OnQuit(wxCommandEvent& event); | |
47 | void OnAbout(wxCommandEvent& event); | |
48 | void OnDelete(wxCommandEvent& event); | |
e3065973 | 49 | void OnCloseWindow(wxCloseEvent& event); |
0be4095a VZ |
50 | |
51 | private: | |
52 | wxTextCtrl *m_text; | |
53 | wxCheckBox *m_check; | |
54 | ||
55 | DECLARE_EVENT_TABLE() | |
56 | }; | |
57 | ||
58 | enum | |
59 | { | |
60 | Minimal_Quit, | |
61 | Minimal_About, | |
62 | Minimal_Delete | |
63 | }; | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // event tables | |
67 | // ---------------------------------------------------------------------------- | |
68 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
69 | EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
70 | EVT_MENU(Minimal_About, MyFrame::OnAbout) | |
71 | EVT_MENU(Minimal_Delete, MyFrame::OnDelete) | |
e3065973 | 72 | EVT_CLOSE(MyFrame::OnCloseWindow) |
0be4095a VZ |
73 | END_EVENT_TABLE() |
74 | ||
75 | // ============================================================================ | |
76 | // implementation | |
77 | // ============================================================================ | |
78 | ||
79 | // ---------------------------------------------------------------------------- | |
80 | // application | |
81 | // ---------------------------------------------------------------------------- | |
82 | IMPLEMENT_APP(MyApp) | |
83 | ||
84 | // `Main program' equivalent, creating windows and returning main app frame | |
85 | bool MyApp::OnInit() | |
86 | { | |
87 | // we're using wxConfig's "create-on-demand" feature: it will create the | |
88 | // config object when it's used for the first time. It has a number of | |
89 | // advantages compared with explicitly creating our wxConfig: | |
90 | // 1) we don't pay for it if we don't use it | |
91 | // 2) there is no danger to create it twice | |
92 | ||
93 | // application and vendor name are used by wxConfig to construct the name | |
94 | // of the config file/registry key and must be set before the first call | |
95 | // to Get() if you want to override the default values (the application | |
96 | // name is the name of the executable and the vendor name is the same) | |
97 | SetVendorName("wxWindows"); | |
98 | SetAppName("conftest"); // not needed, it's the default value | |
99 | ||
100 | wxConfigBase *pConfig = wxConfigBase::Get(); | |
101 | ||
102 | // or you could also write something like this: | |
103 | // wxFileConfig *pConfig = new wxFileConfig("conftest"); | |
104 | // wxConfigBase::Set(pConfig); | |
105 | // where you can also specify the file names explicitly if you wish. | |
106 | // Of course, calling Set() is optional and you only must do it if | |
107 | // you want to later retrieve this pointer with Get(). | |
108 | ||
109 | // create the main program window | |
110 | MyFrame *frame = new MyFrame; | |
111 | frame->Show(TRUE); | |
112 | SetTopWindow(frame); | |
113 | ||
114 | // use our config object... | |
115 | if ( pConfig->Read("/Controls/Check", 1l) != 0 ) { | |
116 | wxMessageBox("You can disable this message box by unchecking\n" | |
117 | "the checkbox in the main window (of course, a real\n" | |
118 | "program would have a checkbox right here but we\n" | |
119 | "keep it simple)", "Welcome to wxConfig demo", | |
120 | wxICON_INFORMATION | wxOK); | |
121 | } | |
122 | ||
123 | return TRUE; | |
124 | } | |
125 | ||
126 | int MyApp::OnExit() | |
127 | { | |
128 | // clean up: Set() returns the active config object as Get() does, but unlike | |
129 | // Get() it doesn't try to create one if there is none (definitely not what | |
130 | // we want here!) | |
c67daf87 | 131 | delete wxConfigBase::Set((wxConfigBase *) NULL); |
0be4095a VZ |
132 | |
133 | return 0; | |
134 | } | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
137 | // frame | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | // main frame ctor | |
141 | MyFrame::MyFrame() | |
c67daf87 | 142 | : wxFrame((wxFrame *) NULL, -1, "wxConfig Demo") |
0be4095a | 143 | { |
f7bdcdd7 RR |
144 | // submenu |
145 | wxMenu *sub_menu = new wxMenu( wxMENU_TEAROFF ); | |
146 | sub_menu->Append(Minimal_About, "&About", "About this sample"); | |
147 | sub_menu->Append(Minimal_About, "&About", "About this sample"); | |
148 | sub_menu->Append(Minimal_About, "&About", "About this sample"); | |
149 | ||
0be4095a VZ |
150 | // menu |
151 | wxMenu *file_menu = new wxMenu; | |
152 | ||
153 | file_menu->Append(Minimal_Delete, "&Delete", "Delete config file"); | |
154 | file_menu->AppendSeparator(); | |
155 | file_menu->Append(Minimal_About, "&About", "About this sample"); | |
156 | file_menu->AppendSeparator(); | |
157 | file_menu->Append(Minimal_Quit, "E&xit", "Exit the program"); | |
158 | wxMenuBar *menu_bar = new wxMenuBar; | |
159 | menu_bar->Append(file_menu, "&File"); | |
160 | SetMenuBar(menu_bar); | |
161 | ||
162 | CreateStatusBar(); | |
163 | ||
164 | // child controls | |
165 | wxPanel *panel = new wxPanel(this); | |
166 | (void)new wxStaticText(panel, -1, "These controls remember their values!", | |
167 | wxPoint(10, 10), wxSize(300, 20)); | |
168 | m_text = new wxTextCtrl(panel, -1, "", wxPoint(10, 40), wxSize(300, 20)); | |
169 | m_check = new wxCheckBox(panel, -1, "show welcome message box at startup", | |
170 | wxPoint(10, 70), wxSize(300, 20)); | |
171 | ||
172 | // restore the control's values from the config | |
173 | ||
174 | // NB: in this program, the config object is already created at this moment | |
175 | // because we had called Get() from MyApp::OnInit(). However, if you later | |
176 | // change the code and don't create it before this line, it won't break | |
177 | // anything - unlike if you manually create wxConfig object with Create() | |
178 | // or in any other way (then you must be sure to create it before using it!). | |
179 | wxConfigBase *pConfig = wxConfigBase::Get(); | |
180 | ||
181 | // we could write Read("/Controls/Text") as well, it's just to show SetPath() | |
182 | pConfig->SetPath("/Controls"); | |
183 | ||
184 | m_text->SetValue(pConfig->Read("Text", "")); | |
185 | m_check->SetValue(pConfig->Read("Check", 1l) != 0); | |
186 | ||
187 | // SetPath() understands ".." | |
188 | pConfig->SetPath("../MainFrame"); | |
189 | ||
190 | // restore frame position and size | |
191 | int x = pConfig->Read("x", 50), | |
192 | y = pConfig->Read("y", 50), | |
193 | w = pConfig->Read("w", 350), | |
194 | h = pConfig->Read("h", 200); | |
195 | Move(x, y); | |
196 | SetClientSize(w, h); | |
197 | } | |
198 | ||
e3065973 JS |
199 | void MyFrame::OnCloseWindow(wxCloseEvent& event) |
200 | { | |
201 | this->Destroy(); | |
202 | } | |
203 | ||
0be4095a VZ |
204 | void MyFrame::OnQuit(wxCommandEvent&) |
205 | { | |
206 | Close(TRUE); | |
207 | } | |
208 | ||
209 | void MyFrame::OnAbout(wxCommandEvent&) | |
210 | { | |
211 |