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