]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: regtest.cpp | |
3 | // Purpose: wxRegKey class demo | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 03.04.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 | #ifdef __BORLANDC__ | |
22 | # pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #ifndef WX_PRECOMP | |
26 | # include "wx/wx.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/treectrl.h" | |
30 | #include "wx/config.h" | |
31 | #include "wx/imaglist.h" | |
32 | #include "wx/tokenzr.h" | |
33 | ||
34 | #if wxUSE_CONFIG_NATIVE && defined( __WXMSW__ ) | |
35 | # define DO_REGTEST 1 | |
36 | #else | |
37 | # define DO_REGTEST 0 | |
38 | #endif | |
39 | ||
40 | // ---------------------------------------------------------------------------- | |
41 | // application type | |
42 | // ---------------------------------------------------------------------------- | |
43 | class RegApp : public wxApp | |
44 | { | |
45 | public: | |
46 | bool OnInit(); | |
47 | }; | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // image list with registry icons | |
51 | // ---------------------------------------------------------------------------- | |
52 | class RegImageList : public wxImageList | |
53 | { | |
54 | public: | |
55 | enum Icon | |
56 | { | |
57 | Root, | |
58 | ClosedKey, | |
59 | OpenedKey, | |
60 | TextValue, | |
61 | BinaryValue | |
62 | }; | |
63 | ||
64 | RegImageList(); | |
65 | }; | |
66 | ||
67 | #if DO_REGTEST | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // our control | |
71 | // ---------------------------------------------------------------------------- | |
72 | class RegTreeCtrl : public wxTreeCtrl | |
73 | { | |
74 | public: | |
75 | // ctor & dtor | |
76 | RegTreeCtrl(wxWindow *parent, wxWindowID id); | |
77 | virtual ~RegTreeCtrl(); | |
78 | ||
79 | // notifications | |
80 | void OnDeleteItem (wxTreeEvent& event); | |
81 | void OnItemExpanding (wxTreeEvent& event); | |
82 | void OnSelChanged (wxTreeEvent& event); | |
83 | ||
84 | void OnBeginEdit (wxTreeEvent& event); | |
85 | void OnEndEdit (wxTreeEvent& event); | |
86 | ||
87 | void OnBeginDrag (wxTreeEvent& event); | |
88 | void OnEndDrag (wxTreeEvent& event); | |
89 | ||
90 | void OnRightClick (wxMouseEvent& event); | |
91 | void OnChar (wxKeyEvent& event); | |
92 | void OnIdle (wxIdleEvent& event); | |
93 | ||
94 | // forwarded notifications (by the frame) | |
95 | void OnMenuTest(); | |
96 | ||
97 | // operations | |
98 | void GoTo(const wxString& location); | |
99 | void DoRefresh(); | |
100 | void DeleteSelected(); | |
101 | void ShowProperties(); | |
102 | void CreateNewKey(const wxString& strName); | |
103 | void CreateNewTextValue(const wxString& strName); | |
104 | void CreateNewBinaryValue(const wxString& strName); | |
105 | ||
106 | // information | |
107 | bool IsKeySelected() const; | |
108 | ||
109 | private: | |
110 | // structure describing a registry key/value | |
111 | class TreeNode : public wxTreeItemData | |
112 | { | |
113 | WX_DEFINE_ARRAY_PTR(TreeNode *, TreeChildren); | |
114 | public: | |
115 | RegTreeCtrl *m_pTree; // must be !NULL | |
116 | TreeNode *m_pParent; // NULL only for the root node | |
117 | long m_id; // the id of the tree control item | |
118 | wxString m_strName; // name of the key/value | |
119 | TreeChildren m_aChildren; // array of subkeys/values | |
120 | bool m_bKey; // key or value? | |
121 | wxRegKey *m_pKey; // only may be !NULL if m_bKey == true | |
122 | ||
123 | // trivial accessors | |
124 | long Id() const { return m_id; } | |
125 | bool IsRoot() const { return m_pParent == NULL; } | |
126 | bool IsKey() const { return m_bKey; } | |
127 | TreeNode *Parent() const { return m_pParent; } | |
128 | ||
129 | // notifications | |
130 | bool OnExpand(); | |
131 | void OnCollapse(); | |
132 | ||
133 | // operations | |
134 | void Refresh(); | |
135 | bool DeleteChild(TreeNode *child); | |
136 | void DestroyChildren(); | |
137 | const wxChar *FullName() const; | |
138 | ||
139 | // get the associated key: make sure the pointer is !NULL | |
140 | wxRegKey& Key() { if ( !m_pKey ) OnExpand(); return *m_pKey; } | |
141 | ||
142 | // dtor deletes all children | |
143 | ~TreeNode(); | |
144 | }; | |
145 | ||
146 | wxImageList *m_imageList; | |
147 | wxMenu *m_pMenuPopup; | |
148 | ||
149 | TreeNode *m_pRoot; | |
150 | ||
151 | TreeNode *m_draggedItem; // the item being dragged | |
152 | bool m_copyOnDrop; // if false, then move | |
153 | ||
154 | bool m_restoreStatus; // after OnItemExpanding() | |
155 | ||
156 | wxString m_nameOld; // the initial value of item being renamed | |
157 | ||
158 | TreeNode *GetNode(const wxTreeEvent& event) | |
159 | { return (TreeNode *)GetItemData((WXHTREEITEM)event.GetItem()); } | |
160 | ||
161 | public: | |
162 | // create a new node and insert it to the tree | |
163 | TreeNode *InsertNewTreeNode(TreeNode *pParent, | |
164 | const wxString& strName, | |
165 | int idImage = RegImageList::ClosedKey, | |
166 | const wxString *pstrValue = NULL); | |
167 | ||
168 | // add standard registry keys | |
169 | void AddStdKeys(); | |
170 | ||
171 | private: | |
172 | DECLARE_EVENT_TABLE() | |
173 | }; | |
174 | ||
175 | #endif // #if DO_REGTEST | |
176 | ||
177 | // ---------------------------------------------------------------------------- | |
178 | // the main window of our application | |
179 | // ---------------------------------------------------------------------------- | |
180 | class RegFrame : public wxFrame | |
181 | { | |
182 | public: | |
183 | // ctor & dtor | |
184 | RegFrame(wxFrame *parent, wxChar *title, int x, int y, int w, int h); | |
185 | virtual ~RegFrame(); | |
186 | ||
187 | // callbacks | |
188 | void OnQuit (wxCommandEvent& event); | |
189 | void OnAbout(wxCommandEvent& event); | |
190 | void OnTest (wxCommandEvent& event); | |
191 | ||
192 | void OnGoTo (wxCommandEvent& event); | |
193 | ||
194 | void OnExpand (wxCommandEvent& event); | |
195 | void OnCollapse(wxCommandEvent& event); | |
196 | void OnToggle (wxCommandEvent& event); | |
197 | void OnRefresh (wxCommandEvent& event); | |
198 | ||
199 | void OnDelete (wxCommandEvent& event); | |
200 | void OnNewKey (wxCommandEvent& event); | |
201 | void OnNewText (wxCommandEvent& event); | |
202 | void OnNewBinary(wxCommandEvent& event); | |
203 | ||
204 | void OnInfo (wxCommandEvent& event); | |
205 | ||
206 | DECLARE_EVENT_TABLE() | |
207 | ||
208 | private: | |
209 | ||
210 | #if DO_REGTEST | |
211 | RegTreeCtrl *m_treeCtrl; | |
212 | #endif | |
213 | }; | |
214 | ||
215 | // ---------------------------------------------------------------------------- | |
216 | // various ids | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
219 | enum | |
220 | { | |
221 | Menu_Quit = 100, | |
222 | Menu_About, | |
223 | Menu_Test, | |
224 | Menu_GoTo, | |
225 | Menu_Expand, | |
226 | Menu_Collapse, | |
227 | Menu_Toggle, | |
228 | Menu_Refresh, | |
229 | Menu_New, | |
230 | Menu_NewKey, | |
231 | Menu_NewText, | |
232 | Menu_NewBinary, | |
233 | Menu_Delete, | |
234 | Menu_Info, | |
235 | ||
236 | Ctrl_RegTree = 200, | |
237 | }; | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // event tables | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | BEGIN_EVENT_TABLE(RegFrame, wxFrame) | |
244 | EVT_MENU(Menu_Test, RegFrame::OnTest) | |
245 | EVT_MENU(Menu_About, RegFrame::OnAbout) | |
246 | EVT_MENU(Menu_Quit, RegFrame::OnQuit) | |
247 | EVT_MENU(Menu_GoTo, RegFrame::OnGoTo) | |
248 | EVT_MENU(Menu_Expand, RegFrame::OnExpand) | |
249 | EVT_MENU(Menu_Collapse, RegFrame::OnCollapse) | |
250 | EVT_MENU(Menu_Toggle, RegFrame::OnToggle) | |
251 | EVT_MENU(Menu_Refresh, RegFrame::OnRefresh) | |
252 | EVT_MENU(Menu_Delete, RegFrame::OnDelete) | |
253 | EVT_MENU(Menu_NewKey, RegFrame::OnNewKey) | |
254 | EVT_MENU(Menu_NewText, RegFrame::OnNewText) | |
255 | EVT_MENU(Menu_NewBinary,RegFrame::OnNewBinary) | |
256 | EVT_MENU(Menu_Info, RegFrame::OnInfo) | |
257 | END_EVENT_TABLE() | |
258 | ||
259 | #if DO_REGTEST | |
260 | ||
261 | BEGIN_EVENT_TABLE(RegTreeCtrl, wxTreeCtrl) | |
262 | EVT_TREE_DELETE_ITEM (Ctrl_RegTree, RegTreeCtrl::OnDeleteItem) | |
263 | EVT_TREE_ITEM_EXPANDING(Ctrl_RegTree, RegTreeCtrl::OnItemExpanding) | |
264 | EVT_TREE_SEL_CHANGED (Ctrl_RegTree, RegTreeCtrl::OnSelChanged) | |
265 | ||
266 | EVT_TREE_BEGIN_LABEL_EDIT(Ctrl_RegTree, RegTreeCtrl::OnBeginEdit) | |
267 | EVT_TREE_END_LABEL_EDIT (Ctrl_RegTree, RegTreeCtrl::OnEndEdit) | |
268 | ||
269 | EVT_TREE_BEGIN_DRAG (Ctrl_RegTree, RegTreeCtrl::OnBeginDrag) | |
270 | EVT_TREE_BEGIN_RDRAG (Ctrl_RegTree, RegTreeCtrl::OnBeginDrag) | |
271 | EVT_TREE_END_DRAG (Ctrl_RegTree, RegTreeCtrl::OnEndDrag) | |
272 | ||
273 | EVT_CHAR (RegTreeCtrl::OnChar) | |
274 | EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick) | |
275 | EVT_IDLE (RegTreeCtrl::OnIdle) | |
276 | END_EVENT_TABLE() | |
277 | ||
278 | #endif | |
279 | ||
280 | // ============================================================================ | |
281 | // implementation | |
282 | // ============================================================================ | |
283 | ||
284 | // ---------------------------------------------------------------------------- | |
285 | // global functions | |
286 | // ---------------------------------------------------------------------------- | |
287 | ||
288 | // create the "registry operations" menu | |
289 | wxMenu *CreateRegistryMenu() | |
290 | { | |
291 | wxMenu *pMenuNew = new wxMenu; | |
292 | pMenuNew->Append(Menu_NewKey, _T("&Key"), _T("Create a new key")); | |
293 | pMenuNew->AppendSeparator(); | |
294 | pMenuNew->Append(Menu_NewText, _T("&Text value"), _T("Create a new text value")); | |
295 | pMenuNew->Append(Menu_NewBinary, _T("&Binary value"), _T("Create a new binary value")); | |
296 | ||
297 | wxMenu *pMenuReg = new wxMenu; | |
298 | pMenuReg->Append(Menu_New, _T("&New"), pMenuNew); | |
299 | pMenuReg->Append(Menu_Delete, _T("&Delete..."), _T("Delete selected key/value")); | |
300 | pMenuReg->AppendSeparator(); | |
301 | pMenuReg->Append(Menu_GoTo, _T("&Go to...\tCtrl-G"), _T("Go to registry key")); | |
302 | pMenuReg->Append(Menu_Expand, _T("&Expand"), _T("Expand current key")); | |
303 | pMenuReg->Append(Menu_Collapse, _T("&Collapse"), _T("Collapse current key")); | |
304 | pMenuReg->Append(Menu_Toggle, _T("&Toggle"), _T("Toggle current key")); | |
305 | pMenuReg->AppendSeparator(); | |
306 | pMenuReg->Append(Menu_Refresh, _T("&Refresh"), _T("Refresh the subtree")); | |
307 | pMenuReg->AppendSeparator(); | |
308 | pMenuReg->Append(Menu_Info, _T("&Properties"),_T("Information about current selection")); | |
309 | ||
310 | return pMenuReg; | |
311 | } | |
312 | ||
313 | // ---------------------------------------------------------------------------- | |
314 | // application class | |
315 | // ---------------------------------------------------------------------------- | |
316 | IMPLEMENT_APP(RegApp) | |
317 | ||
318 | // `Main program' equivalent, creating windows and returning main app frame | |
319 | bool RegApp::OnInit() | |
320 | { | |
321 | // create the main frame window and show it | |
322 | RegFrame *frame = new RegFrame(NULL, _T("wxRegTest"), 50, 50, 600, 350); | |
323 | frame->Show(true); | |
324 | ||
325 | SetTopWindow(frame); | |
326 | ||
327 | return true; | |
328 | } | |
329 | ||
330 | // ---------------------------------------------------------------------------- | |
331 | // RegFrame | |
332 | // ---------------------------------------------------------------------------- | |
333 | ||
334 | RegFrame::RegFrame(wxFrame *parent, wxChar *title, int x, int y, int w, int h) | |
335 | : wxFrame(parent, -1, title, wxPoint(x, y), wxSize(w, h)) | |
336 | { | |
337 | // this reduces flicker effects | |
338 | SetBackgroundColour(wxColour(255, 255, 255)); | |
339 | ||
340 | // set the icon | |
341 | // ------------ | |
342 | SetIcon(wxIcon(_T("app_icon"))); | |
343 | ||
344 | // create menu | |
345 | // ----------- | |
346 | wxMenu *pMenuFile = new wxMenu; | |
347 | pMenuFile->Append(Menu_Test, _T("Te&st"), _T("Test key creation")); | |
348 | pMenuFile->AppendSeparator(); | |
349 | pMenuFile->Append(Menu_About, _T("&About..."), _T("Show an extraordinarly beautiful dialog")); | |
350 | pMenuFile->AppendSeparator(); | |
351 | pMenuFile->Append(Menu_Quit, _T("E&xit"), _T("Quit this program")); | |
352 | ||
353 | wxMenuBar *pMenu = new wxMenuBar; | |
354 | pMenu->Append(pMenuFile, _T("&File")); | |
355 | pMenu->Append(CreateRegistryMenu(), _T("&Registry")); | |
356 | SetMenuBar(pMenu); | |
357 | ||
358 | #if DO_REGTEST | |
359 | // create child controls | |
360 | // --------------------- | |
361 | m_treeCtrl = new RegTreeCtrl(this, Ctrl_RegTree); | |
362 | #endif | |
363 | ||
364 | // create the status line | |
365 | // ---------------------- | |
366 | CreateStatusBar(2); | |
367 | } | |
368 | ||
369 | RegFrame::~RegFrame() | |
370 | { | |
371 | #if DO_REGTEST | |
372 | // this makes deletion of it *much* quicker | |
373 | m_treeCtrl->Hide(); | |
374 | #endif | |
375 | } | |
376 | ||
377 | void RegFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
378 | { | |
379 | Close(true); | |
380 | } | |
381 | ||
382 | void RegFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
383 | { | |
384 | wxMessageDialog dialog(this, | |
385 | _T("wxRegistry sample\n") | |
386 |