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