]>
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 KB |
33 | |
34 | // ---------------------------------------------------------------------------- | |
35 | // application type | |
36 | // ---------------------------------------------------------------------------- | |
37 | class RegApp : public wxApp | |
23f681ec | 38 | { |
bbf1f0e5 | 39 | public: |
23f681ec | 40 | bool OnInit(); |
bbf1f0e5 KB |
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 | ||
23f681ec VZ |
76 | void OnBeginDrag (wxTreeEvent& event); |
77 | void OnEndDrag (wxTreeEvent& event); | |
78 | ||
bbf1f0e5 KB |
79 | void OnRightClick (wxMouseEvent& event); |
80 | void OnChar (wxKeyEvent& event); | |
23f681ec | 81 | void OnIdle (wxIdleEvent& event); |
bbf1f0e5 KB |
82 | |
83 | // forwarded notifications (by the frame) | |
84 | void OnMenuTest(); | |
85 | ||
86 | // operations | |
5888ef1e | 87 | void Refresh(); |
bbf1f0e5 | 88 | void DeleteSelected(); |
23f681ec | 89 | void ShowProperties(); |
bbf1f0e5 KB |
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 | ||
bbf1f0e5 | 97 | private: |
bbf1f0e5 | 98 | // structure describing a registry key/value |
23f681ec | 99 | class TreeNode : public wxTreeItemData |
bbf1f0e5 | 100 | { |
23f681ec | 101 | WX_DEFINE_ARRAY(TreeNode *, TreeChildren); |
42c5812d | 102 | public: |
23f681ec VZ |
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(); | |
bbf1f0e5 KB |
132 | }; |
133 | ||
23f681ec | 134 | wxImageList *m_imageList; |
bbf1f0e5 | 135 | wxMenu *m_pMenuPopup; |
23f681ec | 136 | |
bbf1f0e5 | 137 | TreeNode *m_pRoot; |
23f681ec VZ |
138 | |
139 | TreeNode *m_draggedItem; // the item being dragged | |
140 | bool m_copyOnDrop; // if FALSE, then move | |
141 | ||
142 | bool m_restoreStatus; // after OnItemExpanding() | |
bbf1f0e5 KB |
143 | |
144 | TreeNode *GetNode(const wxTreeEvent& event) | |
42c5812d | 145 | { return (TreeNode *)GetItemData((WXHTREEITEM)event.GetItem()); } |
bbf1f0e5 KB |
146 | |
147 | public: | |
148 | // create a new node and insert it to the tree | |
23f681ec | 149 | TreeNode *InsertNewTreeNode(TreeNode *pParent, |
bbf1f0e5 KB |
150 | const wxString& strName, |
151 | int idImage = RegImageList::ClosedKey, | |
152 | const wxString *pstrValue = NULL); | |
153 | // add standard registry keys | |
154 | void AddStdKeys(); | |
23f681ec VZ |
155 | |
156 | private: | |
157 | DECLARE_EVENT_TABLE(); | |
bbf1f0e5 KB |
158 | }; |
159 | ||
160 | // ---------------------------------------------------------------------------- | |
161 | // the main window of our application | |
162 | // ---------------------------------------------------------------------------- | |
163 | class RegFrame : public wxFrame | |
23f681ec | 164 | { |
bbf1f0e5 KB |
165 | public: |
166 | // ctor & dtor | |
167 | RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h); | |
23f681ec VZ |
168 | virtual ~RegFrame(); |
169 | ||
bbf1f0e5 KB |
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); | |
5888ef1e | 178 | void OnRefresh (wxCommandEvent& event); |
bbf1f0e5 KB |
179 | |
180 | void OnDelete (wxCommandEvent& event); | |
181 | void OnNewKey (wxCommandEvent& event); | |
182 | void OnNewText (wxCommandEvent& event); | |
183 | void OnNewBinary(wxCommandEvent& event); | |
184 | ||
23f681ec | 185 | void OnInfo (wxCommandEvent& event); |
bbf1f0e5 KB |
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, | |
5888ef1e | 205 | Menu_Refresh, |
bbf1f0e5 | 206 | Menu_New, |
23f681ec VZ |
207 | Menu_NewKey, |
208 | Menu_NewText, | |
bbf1f0e5 KB |
209 | Menu_NewBinary, |
210 | Menu_Delete, | |
23f681ec | 211 | Menu_Info, |
bbf1f0e5 KB |
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) | |
5888ef1e | 227 | EVT_MENU(Menu_Refresh, RegFrame::OnRefresh) |
bbf1f0e5 KB |
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) | |
23f681ec | 232 | EVT_MENU(Menu_Info, RegFrame::OnInfo) |
bbf1f0e5 KB |
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) | |
23f681ec VZ |
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) | |
bbf1f0e5 KB |
242 | |
243 | EVT_CHAR (RegTreeCtrl::OnChar) | |
244 | EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick) | |
23f681ec | 245 | EVT_IDLE (RegTreeCtrl::OnIdle) |
bbf1f0e5 KB |
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"); | |
23f681ec | 272 | pMenuReg->AppendSeparator(); |
5888ef1e VZ |
273 | pMenuReg->Append(Menu_Refresh, "&Refresh", "Refresh the subtree"); |
274 | pMenuReg->AppendSeparator(); | |
23f681ec | 275 | pMenuReg->Append(Menu_Info, "&Properties","Information about current selection"); |
bbf1f0e5 KB |
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 | |
23f681ec | 289 | RegFrame *frame = new RegFrame(NULL, "wxRegTest", 50, 50, 600, 350); |
a42b93aa | 290 | frame->Show(TRUE); |
23f681ec | 291 | |
bbf1f0e5 KB |
292 | SetTopWindow(frame); |
293 | ||
a42b93aa | 294 | return TRUE; |
bbf1f0e5 KB |
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 | // ---------------------- | |
bbf1f0e5 | 331 | CreateStatusBar(2); |
bbf1f0e5 KB |
332 | } |
333 | ||
23f681ec | 334 | RegFrame::~RegFrame() |
bbf1f0e5 | 335 | { |
23f681ec VZ |
336 | // this makes deletion of it *much* quicker |
337 | m_treeCtrl->Hide(); | |
bbf1f0e5 KB |
338 | } |
339 | ||
340 | void RegFrame::OnQuit(wxCommandEvent& event) | |
341 | { | |
342 | Close(TRUE); | |
343 | } | |
344 | ||
345 | void RegFrame::OnAbout(wxCommandEvent& event) | |
346 | { | |
23f681ec VZ |
347 | wxMessageDialog dialog(this, |
348 | "wxRegistry sample\n" | |
349 |