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