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