]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: registry.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(void); | |
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 | // array of children of the node | |
62 | //class TreeNode; | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // our control | |
66 | // ---------------------------------------------------------------------------- | |
67 | class RegTreeCtrl : public wxTreeCtrl | |
68 | { | |
69 | public: | |
70 | // ctor & dtor | |
71 | RegTreeCtrl(wxWindow *parent, wxWindowID id); | |
72 | virtual ~RegTreeCtrl(); | |
73 | ||
74 | // notifications | |
75 | void OnDeleteItem (wxTreeEvent& event); | |
76 | void OnItemExpanding(wxTreeEvent& event); | |
77 | void OnSelChanged (wxTreeEvent& event); | |
78 | ||
79 | void OnRightClick (wxMouseEvent& event); | |
80 | void OnChar (wxKeyEvent& event); | |
81 | ||
82 | // forwarded notifications (by the frame) | |
83 | void OnMenuTest(); | |
84 | ||
85 | // operations | |
86 | void DeleteSelected(); | |
87 | void CreateNewKey(const wxString& strName); | |
88 | void CreateNewTextValue(const wxString& strName); | |
89 | void CreateNewBinaryValue(const wxString& strName); | |
90 | ||
91 | // information | |
92 | bool IsKeySelected() const; | |
93 | ||
94 | DECLARE_EVENT_TABLE(); | |
95 | ||
96 | private: | |
97 | ||
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 | long m_lDummy; // dummy subkey (to make expansion possible) | |
111 | ||
112 | // ctor | |
113 | TreeNode() { m_lDummy = 0; } | |
114 | ||
115 | // trivial accessors | |
116 | long Id() const { return m_id; } | |
117 | bool IsRoot() const { return m_pParent == NULL; } | |
118 | bool IsKey() const { return m_bKey; } | |
119 | TreeNode *Parent() const { return m_pParent; } | |
120 | ||
121 | // notifications | |
122 | bool OnExpand(); | |
123 | void OnCollapse(); | |
124 | ||
125 | // operations | |
126 | void Refresh() { OnCollapse(); OnExpand(); } | |
127 | void AddDummy(); | |
128 | void DestroyChildren(); | |
129 | const char *FullName() const; | |
130 | ||
131 | // get the associated key: make sure the pointer is !NULL | |
132 | wxRegKey& Key() { if ( !m_pKey ) OnExpand(); return *m_pKey; } | |
133 | ||
134 | // dtor deletes all children | |
135 | ~TreeNode(); | |
136 | }; | |
137 | ||
138 | wxMenu *m_pMenuPopup; | |
139 | TreeNode *m_pRoot; | |
140 | wxImageList *m_imageList; | |
141 | ||
142 | TreeNode *GetNode(const wxTreeEvent& event) | |
143 | { return (TreeNode *)GetItemData((WXHTREEITEM)event.GetItem()); } | |
144 | ||
145 | public: | |
146 | // create a new node and insert it to the tree | |
147 | TreeNode *InsertNewTreeNode(TreeNode *pParent, | |
148 | const wxString& strName, | |
149 | int idImage = RegImageList::ClosedKey, | |
150 | const wxString *pstrValue = NULL); | |
151 | // add standard registry keys | |
152 | void AddStdKeys(); | |
153 | }; | |
154 | ||
155 | // ---------------------------------------------------------------------------- | |
156 | // the main window of our application | |
157 | // ---------------------------------------------------------------------------- | |
158 | class RegFrame : public wxFrame | |
159 | { | |
160 | public: | |
161 | // ctor & dtor | |
162 | RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h); | |
163 | virtual ~RegFrame(void); | |
164 | ||
165 | // callbacks | |
166 | void OnQuit (wxCommandEvent& event); | |
167 | void OnAbout(wxCommandEvent& event); | |
168 | void OnTest (wxCommandEvent& event); | |
169 | ||
170 | void OnExpand (wxCommandEvent& event); | |
171 | void OnCollapse(wxCommandEvent& event); | |
172 | void OnToggle (wxCommandEvent& event); | |
173 | ||
174 | void OnDelete (wxCommandEvent& event); | |
175 | void OnNewKey (wxCommandEvent& event); | |
176 | void OnNewText (wxCommandEvent& event); | |
177 | void OnNewBinary(wxCommandEvent& event); | |
178 | ||
179 | bool OnClose () { return TRUE; } | |
180 | ||
181 | DECLARE_EVENT_TABLE(); | |
182 | ||
183 | private: | |
184 | RegTreeCtrl *m_treeCtrl; | |
185 | }; | |
186 | ||
187 | // ---------------------------------------------------------------------------- | |
188 | // various ids | |
189 | // ---------------------------------------------------------------------------- | |
190 | ||
191 | enum | |
192 | { | |
193 | Menu_Quit = 100, | |
194 | Menu_About, | |
195 | Menu_Test, | |
196 | Menu_Expand, | |
197 | Menu_Collapse, | |
198 | Menu_Toggle, | |
199 | Menu_New, | |
200 | Menu_NewKey, | |
201 | Menu_NewText, | |
202 | Menu_NewBinary, | |
203 | Menu_Delete, | |
204 | ||
205 | Ctrl_RegTree = 200, | |
206 | }; | |
207 | ||
208 | // ---------------------------------------------------------------------------- | |
209 | // event tables | |
210 | // ---------------------------------------------------------------------------- | |
211 | ||
212 | BEGIN_EVENT_TABLE(RegFrame, wxFrame) | |
213 | EVT_MENU(Menu_Test, RegFrame::OnTest) | |
214 | EVT_MENU(Menu_About, RegFrame::OnAbout) | |
215 | EVT_MENU(Menu_Quit, RegFrame::OnQuit) | |
216 | EVT_MENU(Menu_Expand, RegFrame::OnExpand) | |
217 | EVT_MENU(Menu_Collapse, RegFrame::OnCollapse) | |
218 | EVT_MENU(Menu_Toggle, RegFrame::OnToggle) | |
219 | EVT_MENU(Menu_Delete, RegFrame::OnDelete) | |
220 | EVT_MENU(Menu_NewKey, RegFrame::OnNewKey) | |
221 | EVT_MENU(Menu_NewText, RegFrame::OnNewText) | |
222 | EVT_MENU(Menu_NewBinary,RegFrame::OnNewBinary) | |
223 | END_EVENT_TABLE() | |
224 | ||
225 | BEGIN_EVENT_TABLE(RegTreeCtrl, wxTreeCtrl) | |
226 | EVT_TREE_DELETE_ITEM (Ctrl_RegTree, RegTreeCtrl::OnDeleteItem) | |
227 | EVT_TREE_ITEM_EXPANDING(Ctrl_RegTree, RegTreeCtrl::OnItemExpanding) | |
228 | EVT_TREE_SEL_CHANGED (Ctrl_RegTree, RegTreeCtrl::OnSelChanged) | |
229 | ||
230 | EVT_CHAR (RegTreeCtrl::OnChar) | |
231 | EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick) | |
232 | END_EVENT_TABLE() | |
233 | ||
234 | // ============================================================================ | |
235 | // implementation | |
236 | // ============================================================================ | |
237 | ||
238 | // ---------------------------------------------------------------------------- | |
239 | // global functions | |
240 | // ---------------------------------------------------------------------------- | |
241 | ||
242 | // create the "registry operations" menu | |
243 | wxMenu *CreateRegistryMenu() | |
244 | { | |
245 | wxMenu *pMenuNew = new wxMenu; | |
246 | pMenuNew->Append(Menu_NewKey, "&Key", "Create a new key"); | |
247 | pMenuNew->AppendSeparator(); | |
248 | pMenuNew->Append(Menu_NewText, "&Text value", "Create a new text value"); | |
249 | pMenuNew->Append(Menu_NewBinary, "&Binary value", "Create a new binary value"); | |
250 | ||
251 | wxMenu *pMenuReg = new wxMenu; | |
252 | pMenuReg->Append(Menu_New, "&New", pMenuNew); | |
253 | pMenuReg->Append(Menu_Delete, "&Delete...", "Delete selected key/value"); | |
254 | pMenuReg->AppendSeparator(); | |
255 | pMenuReg->Append(Menu_Expand, "&Expand", "Expand current key"); | |
256 | pMenuReg->Append(Menu_Collapse, "&Collapse", "Collapse current key"); | |
257 | pMenuReg->Append(Menu_Toggle, "&Toggle", "Toggle current key"); | |
258 | ||
259 | return pMenuReg; | |
260 | } | |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // application class | |
264 | // ---------------------------------------------------------------------------- | |
265 | IMPLEMENT_APP(RegApp) | |
266 | ||
267 | // `Main program' equivalent, creating windows and returning main app frame | |
268 | bool RegApp::OnInit() | |
269 | { | |
270 | // create the main frame window and show it | |
271 | RegFrame *frame = new RegFrame(NULL, "wxRegKey Test", 50, 50, 600, 350); | |
272 | frame->Show(true); | |
273 | ||
274 | SetTopWindow(frame); | |
275 | ||
276 | return true; | |
277 | } | |
278 | ||
279 | // ---------------------------------------------------------------------------- | |
280 | // RegFrame | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | RegFrame::RegFrame(wxFrame *parent, char *title, int x, int y, int w, int h) | |
284 | : wxFrame(parent, -1, title, wxPoint(x, y), wxSize(w, h)) | |
285 | { | |
286 | // this reduces flicker effects | |
287 | SetBackgroundColour(wxColour(255, 255, 255)); | |
288 | ||
289 | // set the icon | |
290 | // ------------ | |
291 | SetIcon(wxIcon("app_icon")); | |
292 | ||
293 | // create menu | |
294 | // ----------- | |
295 | wxMenu *pMenuFile = new wxMenu; | |
296 | pMenuFile->Append(Menu_Test, "Te&st", "Test key creation"); | |
297 | pMenuFile->AppendSeparator(); | |
298 | pMenuFile->Append(Menu_About, "&About...", "Show an extraordinarly beautiful dialog"); | |
299 | pMenuFile->AppendSeparator(); | |
300 | pMenuFile->Append(Menu_Quit, "E&xit", "Quit this program"); | |
301 | ||
302 | wxMenuBar *pMenu = new wxMenuBar; | |
303 | pMenu->Append(pMenuFile, "&File"); | |
304 | pMenu->Append(CreateRegistryMenu(), "&Registry"); | |
305 | SetMenuBar(pMenu); | |
306 | ||
307 | // create child controls | |
308 | // --------------------- | |
309 | m_treeCtrl = new RegTreeCtrl(this, Ctrl_RegTree); | |
310 | ||
311 | // create the status line | |
312 | // ---------------------- | |
313 | int aWidths[2]; | |
314 | aWidths[0] = 200; | |
315 | aWidths[1] = -1; | |
316 | CreateStatusBar(2); | |
317 | SetStatusWidths(2, aWidths); | |
318 | } | |
319 | ||
320 | RegFrame::~RegFrame(void) | |
321 | { | |
322 | } | |
323 | ||
324 | void RegFrame::OnQuit(wxCommandEvent& event) | |
325 | { | |
326 | Close(TRUE); | |
327 | } | |
328 | ||
329 | void RegFrame::OnAbout(wxCommandEvent& event) | |
330 | { | |
331 | wxMessageDialog dialog(this, "wxRegistry sample\n(c) 1998 Vadim Zeitlin", | |
332 | "About wxRegistry", wxOK); | |
333 | ||
334 | dialog.ShowModal(); | |
335 | } | |
336 | ||
337 | void RegFrame::OnTest(wxCommandEvent& event) | |
338 | { | |
339 | m_treeCtrl->OnMenuTest(); | |
340 | } | |
341 | ||
342 | void RegFrame::OnExpand(wxCommandEvent& event) | |
343 | { | |
344 | m_treeCtrl->ExpandItem(m_treeCtrl->GetSelection(), wxTREE_EXPAND_EXPAND); | |
345 | } | |
346 | ||
347 | void RegFrame::OnCollapse(wxCommandEvent& event) | |
348 | { | |
349 | m_treeCtrl->ExpandItem(m_treeCtrl->GetSelection(), wxTREE_EXPAND_COLLAPSE); | |
350 | } | |
351 | ||
352 | void RegFrame::OnToggle(wxCommandEvent& event) | |
353 | { | |
354 | m_treeCtrl->ExpandItem(m_treeCtrl->GetSelection(), wxTREE_EXPAND_TOGGLE); | |
355 | } | |
356 | ||
357 | void RegFrame::OnDelete(wxCommandEvent& event) | |
358 | { | |
359 | m_treeCtrl->DeleteSelected(); | |
360 | } | |
361 | ||
362 | void RegFrame::OnNewKey(wxCommandEvent& event) | |
363 | { | |
364 | if ( m_treeCtrl->IsKeySelected() ) { | |
365 | m_treeCtrl->CreateNewKey( | |
366 | wxGetTextFromUser("Enter the name of the new key")); | |
367 | } | |
368 | } | |
369 | ||
370 | void RegFrame::OnNewText(wxCommandEvent& event) | |
371 | { | |
372 | if ( m_treeCtrl->IsKeySelected() ) { | |
373 | m_treeCtrl->CreateNewTextValue( | |
374 | wxGetTextFromUser("Enter the name for the new text value")); | |
375 | } | |
376 | } | |
377 | ||
378 | void RegFrame::OnNewBinary(wxCommandEvent& event) | |
379 | { | |
380 | if ( m_treeCtrl->IsKeySelected() ) { | |
381 | m_treeCtrl->CreateNewBinaryValue( | |
382 | wxGetTextFromUser("Enter the name for the new binary value")); | |
383 | } | |
384 | } | |
385 | ||
386 | // ---------------------------------------------------------------------------- | |
387 | // RegImageList | |
388 | // ---------------------------------------------------------------------------- | |
389 | RegImageList::RegImageList() : wxImageList(16, 16, TRUE) | |
390 | { | |
391 | // should be in sync with enum RegImageList::RegIcon | |
392 | static const char *aszIcons[] = { "key1","key2","key3","value1","value2" }; | |
393 | wxString str = "icon_"; | |
394 | for ( unsigned int n = 0; n < WXSIZEOF(aszIcons); n++ ) { | |
395 | Add(wxIcon(str + aszIcons[n], wxBITMAP_TYPE_ICO_RESOURCE)); | |
396 | } | |
397 | } | |
398 | ||
399 | // ---------------------------------------------------------------------------- | |
400 | // RegTreeCtrl | |
401 | // ---------------------------------------------------------------------------- | |
402 | ||
403 | // create a new tree item and insert it into the tree | |
404 | RegTreeCtrl::TreeNode *RegTreeCtrl::InsertNewTreeNode(TreeNode *pParent, | |
405 | const wxString& strName, | |
406 | int idImage, | |
407 | const wxString *pstrValue) | |
408 | { | |
409 | // create new item & insert it | |
410 | TreeNode *pNewNode = new TreeNode; | |
411 | pNewNode->m_pTree = this; | |
412 | pNewNode->m_pParent = pParent; | |
413 | pNewNode->m_strName = strName; | |
414 | pNewNode->m_bKey = pstrValue == NULL; | |
415 | pNewNode->m_pKey = NULL; | |
416 | pNewNode->m_id = InsertItem(pParent ? pParent->m_id : 0, | |
417 | pNewNode->IsKey() ? strName : *pstrValue, | |
418 | idImage); | |
419 | ||
420 | wxASSERT_MSG( pNewNode->m_id, "can't create tree control item!"); | |
421 | ||
422 | // save the pointer in the item | |
423 | SetItemData(pNewNode->m_id, pNewNode); | |
424 | ||
425 | // add it to the list of parent's children | |
426 | if ( pParent != NULL ) { | |
427 | pParent->m_aChildren.Add(pNewNode); | |
428 | } | |
429 | ||
430 | // force the [+] button (@@@ not very elegant...) | |
431 | if ( pNewNode->IsKey() ) | |
432 | pNewNode->AddDummy(); | |
433 | ||
434 | return pNewNode; | |
435 | } | |
436 | ||
437 | RegTreeCtrl::RegTreeCtrl(wxWindow *parent, wxWindowID id) | |
438 | : wxTreeCtrl(parent, id, wxDefaultPosition, wxDefaultSize, | |
439 | wxTR_HAS_BUTTONS | wxSUNKEN_BORDER) | |
440 | { | |
441 | // create the image list | |
442 | // --------------------- | |
443 | m_imageList = new RegImageList; | |
444 | SetImageList(m_imageList, wxIMAGE_LIST_NORMAL); | |
445 | ||
446 | // create root keys | |
447 | // ---------------- | |
448 | m_pRoot = InsertNewTreeNode(NULL, "Registry Root", RegImageList::Root); | |
449 | ||
450 | // create popup menu | |
451 | // ----------------- | |
452 | m_pMenuPopup = CreateRegistryMenu(); | |
453 | } | |
454 | ||
455 | RegTreeCtrl::~RegTreeCtrl() | |
456 | { | |
457 | delete m_pMenuPopup; | |
458 | delete m_pRoot; | |
459 | delete m_imageList; | |
460 | } | |
461 | ||
462 | void RegTreeCtrl::AddStdKeys() | |
463 | { | |
464 | for ( unsigned int ui = 0; ui < wxRegKey::nStdKeys; ui++ ) { | |
465 | InsertNewTreeNode(m_pRoot, wxRegKey::GetStdKeyName(ui)); | |
466 | } | |
467 | } | |
468 | ||
469 | // ---------------------------------------------------------------------------- | |
470 | // notifications | |
471 | // ---------------------------------------------------------------------------- | |
472 | ||
473 | void RegTreeCtrl::OnRightClick(wxMouseEvent& event) | |
474 | { | |
475 | int iFlags; | |
476 | long lId = HitTest(wxPoint(event.GetX(), event.GetY()), iFlags); | |
477 | if ( iFlags & wxTREE_HITTEST_ONITEMLABEL ) { | |
478 | // popup menu only if an item was clicked | |
479 | wxASSERT( lId != 0 ); | |
480 | SelectItem(lId); | |
481 | PopupMenu(m_pMenuPopup, event.GetX(), event.GetY()); | |
482 | } | |
483 | } | |
484 | ||
485 | ||
486 | void RegTreeCtrl::OnDeleteItem(wxTreeEvent& event) | |
487 | { | |
488 | } | |
489 | ||
490 | // test the key creation functions | |
491 | void RegTreeCtrl::OnMenuTest() | |
492 | { | |
493 | long lId = GetSelection(); | |
494 | TreeNode *pNode = (TreeNode *)GetItemData(lId); | |
495 | ||
496 | wxCHECK_RET( pNode != NULL, "tree item without data?" ); | |
497 | ||
498 | if ( pNode->IsRoot() ) { | |
499 | wxLogError("Can't create a subkey under the root key."); | |
500 | return; | |
501 | } | |
502 | if ( !pNode->IsKey() ) { | |
503 | wxLogError("Can't create a subkey under a value!"); | |
504 | return; | |
505 | } | |
506 | ||
507 | wxRegKey key1(pNode->Key(), "key1"); | |
508 | if ( key1.Create() ) { | |
509 | wxRegKey key2a(key1, "key2a"), key2b(key1, "key2b"); | |
510 | if ( key2a.Create() && key2b.Create() ) { | |
511 | // put some values under the newly created keys | |
512 | key1.SetValue("first_term", "10"); | |
513 | key1.SetValue("second_term", "7"); | |
514 | key2a = "this is the unnamed value"; | |
515 | key2b.SetValue("sum", 17); | |
516 | ||
517 | // refresh tree | |
518 | pNode->Refresh(); | |
519 | wxLogStatus("Test keys successfully added."); | |
520 | return; | |
521 | } | |
522 | } | |
523 | ||
524 | wxLogError("Creation of test keys failed."); | |
525 | } | |
526 | ||
527 | void RegTreeCtrl::OnChar(wxKeyEvent& event) | |
528 | { | |
529 | if ( event.KeyCode() == WXK_DELETE ) | |
530 | DeleteSelected(); | |
531 | else | |
532 | wxTreeCtrl::OnChar(event); | |
533 | } | |
534 | ||
535 | void RegTreeCtrl::OnSelChanged(wxTreeEvent& event) | |
536 | { | |
537 | wxFrame *pFrame = (wxFrame *)(wxWindow::GetParent()); | |
538 | pFrame->SetStatusText(GetNode(event)->FullName(), 1); | |
539 | } | |
540 | ||
541 | void RegTreeCtrl::OnItemExpanding(wxTreeEvent& event) | |
542 | { | |
543 | TreeNode *pNode = GetNode(event); | |
544 | bool bExpanding = event.GetCode() == wxTREE_EXPAND_EXPAND; | |
545 | ||
546 | // expansion might take some time | |
547 | wxSetCursor(*wxHOURGLASS_CURSOR); | |
548 | wxLogStatus("Working..."); | |
549 | wxYield(); // to give the status line a chance to refresh itself | |
550 | ||
551 | if ( pNode->IsKey() ) { | |
552 | if ( bExpanding ) { | |
553 | // expanding: add subkeys/values | |
554 | if ( !pNode->OnExpand() ) | |
555 | return; | |
556 | } | |
557 | else { | |
558 | // collapsing: clean up | |
559 | pNode->OnCollapse(); | |
560 | } | |
561 | ||
562 | // change icon for non root key | |
563 | if ( !pNode->IsRoot() ) { | |
564 | int idIcon = bExpanding ? RegImageList::OpenedKey | |
565 | : RegImageList::ClosedKey; | |
566 | SetItemImage(pNode->Id(), idIcon); | |
567 | } | |
568 | } | |
569 | ||
570 | wxLogStatus("Ok"); | |
571 | wxSetCursor(*wxSTANDARD_CURSOR); | |
572 | } | |
573 | ||
574 | // ---------------------------------------------------------------------------- | |
575 | // TreeNode implementation | |
576 | // ---------------------------------------------------------------------------- | |
577 | bool RegTreeCtrl::TreeNode::OnExpand() | |
578 | { | |
579 | // remove dummy item | |
580 | if ( m_lDummy != 0 ) { | |
581 | m_pTree->Delete(m_lDummy); | |
582 | m_lDummy = 0; | |
583 | } | |
584 | else { | |
585 | // we've been already expanded | |
586 | return TRUE; | |
587 | } | |
588 | ||
589 | if ( IsRoot() ) { | |
590 | // we're the root key | |
591 | m_pTree->AddStdKeys(); | |
592 | return true; | |
593 | } | |
594 | ||
595 | if ( Parent()->IsRoot() ) { | |
596 | // we're a standard key | |
597 | m_pKey = new wxRegKey(m_strName); | |
598 | } | |
599 | else { | |
600 | // we're a normal key | |
601 | m_pKey = new wxRegKey(*(Parent()->m_pKey), m_strName); | |
602 | } | |
603 | ||
604 | if ( !m_pKey->Open() ) { | |
605 | wxLogError("The key '%s' can't be opened.", FullName()); | |
606 | return false; | |
607 | } | |
608 | ||
609 | // enumeration variables | |
610 | long l; | |
611 | wxString str; | |
612 | bool bCont; | |
613 | ||
614 | // enumerate all subkeys | |
615 | bCont = m_pKey->GetFirstKey(str, l); | |
616 | while ( bCont ) { | |
617 | m_pTree->InsertNewTreeNode(this, str, RegImageList::ClosedKey); | |
618 | bCont = m_pKey->GetNextKey(str, l); | |
619 | } | |
620 | ||
621 | // enumerate all values | |
622 | bCont = m_pKey->GetFirstValue(str, l); | |
623 | while ( bCont ) { | |
624 | wxString strItem; | |
625 | if (str.IsEmpty()) | |
626 | strItem = "<default>"; | |
627 | else | |
628 | strItem = str; | |
629 | strItem += " = "; | |
630 | ||
631 | // determine the appropriate icon | |
632 | RegImageList::Icon icon; | |
633 | switch ( m_pKey->GetValueType(str) ) { | |
634 | case wxRegKey::Type_String: | |
635 | case wxRegKey::Type_Expand_String: | |
636 | case wxRegKey::Type_Multi_String: | |
637 | { | |
638 | wxString strValue; | |
639 | icon = RegImageList::TextValue; | |
640 | m_pKey->QueryValue(str, strValue); | |
641 | strItem += strValue; | |
642 | } | |
643 | break; | |
644 | ||
645 | case wxRegKey::Type_None: | |
646 | // @@ handle the error... | |
647 | icon = RegImageList::BinaryValue; | |
648 | break; | |
649 | ||
650 | case wxRegKey::Type_Dword: | |
651 | { | |
652 | char szBuf[128]; | |
653 | long l; | |
654 | m_pKey->QueryValue(str, &l); | |
655 | sprintf(szBuf, "%lx", l); | |
656 | strItem += szBuf; | |
657 | } | |
658 | ||
659 | // fall through | |
660 | ||
661 | default: | |
662 | icon = RegImageList::BinaryValue; | |
663 | } | |
664 | ||
665 | m_pTree->InsertNewTreeNode(this, str, icon, &strItem); | |
666 | bCont = m_pKey->GetNextValue(str, l); | |
667 | } | |
668 | ||
669 | return true; | |
670 | } | |
671 | ||
672 | void RegTreeCtrl::TreeNode::OnCollapse() | |
673 | { | |
674 | bool bHasChildren = !m_aChildren.IsEmpty(); | |
675 | DestroyChildren(); | |
676 | if ( bHasChildren ) | |
677 | AddDummy(); | |
678 | else | |
679 | m_lDummy = 0; | |
680 | ||
681 | delete m_pKey; | |
682 | m_pKey = NULL; | |
683 | } | |
684 | ||
685 | void RegTreeCtrl::TreeNode::AddDummy() | |
686 | { | |
687 | // insert dummy item forcing appearance of [+] button | |
688 | m_lDummy = m_pTree->InsertItem(Id(), ""); | |
689 | } | |
690 | ||
691 | void RegTreeCtrl::TreeNode::DestroyChildren() | |
692 | { | |
693 | // destroy all children | |
694 | unsigned int nCount = m_aChildren.Count(); | |
695 | for ( unsigned int n = 0; n < nCount; n++ ) { | |
696 | long lId = m_aChildren[n]->Id(); | |
697 | delete m_aChildren[n]; | |
698 | m_pTree->Delete(lId); | |
699 | } | |
700 | ||
701 | m_aChildren.Empty(); | |
702 | } | |
703 | ||
704 | RegTreeCtrl::TreeNode::~TreeNode() | |
705 | { | |
706 | DestroyChildren(); | |
707 | ||
708 | delete m_pKey; | |
709 | } | |
710 | ||
711 | const char *RegTreeCtrl::TreeNode::FullName() const | |
712 | { | |
713 | static wxString s_strName; | |
714 | ||
715 | if ( IsRoot() ) { | |
716 | return "Registry Root"; | |
717 | } | |
718 | else { | |
719 | // our own registry key might not (yet) exist or we might be a value, | |
720 | // so just use the parent's and concatenate | |
721 | s_strName = Parent()->FullName(); | |
722 | s_strName << '\\' << m_strName; | |
723 | ||
724 | return s_strName; | |
725 | } | |
726 | } | |
727 | ||
728 | // ---------------------------------------------------------------------------- | |
729 | // operations on RegTreeCtrl | |
730 | // ---------------------------------------------------------------------------- | |
731 | ||
732 | void RegTreeCtrl::DeleteSelected() | |
733 | { | |
734 | long lCurrent = GetSelection(), | |
735 | lParent = GetParent(lCurrent); | |
736 | ||
737 | if ( lParent == 0 ) { | |
738 | wxLogError("Can't delete root key."); | |
739 | return; | |
740 | } | |
741 | ||
742 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent), | |
743 | *pParent = (TreeNode *)GetItemData(lParent); | |
744 | ||
745 | wxCHECK_RET( pCurrent && pParent, "either node or parent without data?" ); | |
746 | ||
747 | if ( pParent->IsRoot() ) { | |
748 | wxLogError("Can't delete standard key."); | |
749 | return; | |
750 | } | |
751 | ||
752 | if ( pCurrent->IsKey() ) { | |
753 | if ( wxMessageBox("Do you really want to delete this key?", | |
754 | "Confirmation", | |
755 | wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) { | |
756 | return; | |
757 | } | |
758 | ||
759 | // must close key before deleting it | |
760 | pCurrent->OnCollapse(); | |
761 | ||
762 | if ( pParent->Key().DeleteKey(pCurrent->m_strName) ) | |
763 | pParent->Refresh(); | |
764 | } | |
765 | else { | |
766 | if ( wxMessageBox("Do you really want to delete this value?", | |
767 | "Confirmation", | |
768 | wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) { | |
769 | return; | |
770 | } | |
771 | ||
772 | if ( pParent->Key().DeleteValue(pCurrent->m_strName) ) | |
773 | pParent->Refresh(); | |
774 | } | |
775 | } | |
776 | ||
777 | void RegTreeCtrl::CreateNewKey(const wxString& strName) | |
778 | { | |
779 | long lCurrent = GetSelection(); | |
780 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
781 | ||
782 | wxCHECK_RET( pCurrent != NULL, "node without data?" ); | |
783 | ||
784 | wxASSERT( pCurrent->IsKey() ); // check must have been done before | |
785 | ||
786 | if ( pCurrent->IsRoot() ) { | |
787 | wxLogError("Can't create a new key under the root key."); | |
788 | return; | |
789 | } | |
790 | ||
791 | wxRegKey key(pCurrent->Key(), strName); | |
792 | if ( key.Create() ) | |
793 | pCurrent->Refresh(); | |
794 | } | |
795 | ||
796 | void RegTreeCtrl::CreateNewTextValue(const wxString& strName) | |
797 | { | |
798 | long lCurrent = GetSelection(); | |
799 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
800 | ||
801 | wxCHECK_RET( pCurrent != NULL, "node without data?" ); | |
802 | ||
803 | wxASSERT( pCurrent->IsKey() ); // check must have been done before | |
804 | ||
805 | if ( pCurrent->IsRoot() ) { | |
806 | wxLogError("Can't create a new value under the root key."); | |
807 | return; | |
808 | } | |
809 | ||
810 | if ( pCurrent->Key().SetValue(strName, "") ) | |
811 | pCurrent->Refresh(); | |
812 | } | |
813 | ||
814 | void RegTreeCtrl::CreateNewBinaryValue(const wxString& strName) | |
815 | { | |
816 | long lCurrent = GetSelection(); | |
817 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
818 | ||
819 | wxCHECK_RET( pCurrent != NULL, "node without data?" ); | |
820 | ||
821 | wxASSERT( pCurrent->IsKey() ); // check must have been done before | |
822 | ||
823 | if ( pCurrent->IsRoot() ) { | |
824 | wxLogError("Can't create a new value under the root key."); | |
825 | return; | |
826 | } | |
827 | ||
828 | if ( pCurrent->Key().SetValue(strName, 0) ) | |
829 | pCurrent->Refresh(); | |
830 | } | |
831 | ||
832 | bool RegTreeCtrl::IsKeySelected() const | |
833 | { | |
834 | long lCurrent = GetSelection(); | |
835 | TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); | |
836 | ||
837 | wxCHECK( pCurrent != NULL, false ); | |
838 | ||
839 | return pCurrent->IsKey(); | |
840 | } |