1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxRegKey class demo
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
30 #include "wx/treectrl.h"
31 #include "wx/msw/registry.h"
32 #include "wx/msw/imaglist.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
37 class RegApp
: public wxApp
43 // ----------------------------------------------------------------------------
44 // image list with registry icons
45 // ----------------------------------------------------------------------------
46 class RegImageList
: public wxImageList
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
64 class RegTreeCtrl
: public wxTreeCtrl
68 RegTreeCtrl(wxWindow
*parent
, wxWindowID id
);
69 virtual ~RegTreeCtrl();
72 void OnDeleteItem (wxTreeEvent
& event
);
73 void OnItemExpanding(wxTreeEvent
& event
);
74 void OnSelChanged (wxTreeEvent
& event
);
76 void OnBeginDrag (wxTreeEvent
& event
);
77 void OnEndDrag (wxTreeEvent
& event
);
79 void OnRightClick (wxMouseEvent
& event
);
80 void OnChar (wxKeyEvent
& event
);
81 void OnIdle (wxIdleEvent
& event
);
83 // forwarded notifications (by the frame)
88 void DeleteSelected();
89 void ShowProperties();
90 void CreateNewKey(const wxString
& strName
);
91 void CreateNewTextValue(const wxString
& strName
);
92 void CreateNewBinaryValue(const wxString
& strName
);
95 bool IsKeySelected() const;
98 // structure describing a registry key/value
99 class TreeNode
: public wxTreeItemData
101 WX_DEFINE_ARRAY(TreeNode
*, TreeChildren
);
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
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
; }
123 bool DeleteChild(TreeNode
*child
);
124 void DestroyChildren();
125 const char *FullName() const;
127 // get the associated key: make sure the pointer is !NULL
128 wxRegKey
& Key() { if ( !m_pKey
) OnExpand(); return *m_pKey
; }
130 // dtor deletes all children
134 wxImageList
*m_imageList
;
135 wxMenu
*m_pMenuPopup
;
139 TreeNode
*m_draggedItem
; // the item being dragged
140 bool m_copyOnDrop
; // if FALSE, then move
142 bool m_restoreStatus
; // after OnItemExpanding()
144 TreeNode
*GetNode(const wxTreeEvent
& event
)
145 { return (TreeNode
*)GetItemData((WXHTREEITEM
)event
.GetItem()); }
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
157 DECLARE_EVENT_TABLE();
160 // ----------------------------------------------------------------------------
161 // the main window of our application
162 // ----------------------------------------------------------------------------
163 class RegFrame
: public wxFrame
167 RegFrame(wxFrame
*parent
, char *title
, int x
, int y
, int w
, int h
);
171 void OnQuit (wxCommandEvent
& event
);
172 void OnAbout(wxCommandEvent
& event
);
173 void OnTest (wxCommandEvent
& event
);
175 void OnExpand (wxCommandEvent
& event
);
176 void OnCollapse(wxCommandEvent
& event
);
177 void OnToggle (wxCommandEvent
& event
);
178 void OnRefresh (wxCommandEvent
& event
);
180 void OnDelete (wxCommandEvent
& event
);
181 void OnNewKey (wxCommandEvent
& event
);
182 void OnNewText (wxCommandEvent
& event
);
183 void OnNewBinary(wxCommandEvent
& event
);
185 void OnInfo (wxCommandEvent
& event
);
187 DECLARE_EVENT_TABLE();
190 RegTreeCtrl
*m_treeCtrl
;
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
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
)
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
)
243 EVT_CHAR (RegTreeCtrl::OnChar
)
244 EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick
)
245 EVT_IDLE (RegTreeCtrl::OnIdle
)
248 // ============================================================================
250 // ============================================================================
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 // create the "registry operations" menu
257 wxMenu
*CreateRegistryMenu()
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");
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");
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
283 IMPLEMENT_APP(RegApp
)
285 // `Main program' equivalent, creating windows and returning main app frame
286 bool RegApp::OnInit()
288 // create the main frame window and show it
289 RegFrame
*frame
= new RegFrame(NULL
, "wxRegTest", 50, 50, 600, 350);
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
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
))
304 // this reduces flicker effects
305 SetBackgroundColour(wxColour(255, 255, 255));
309 SetIcon(wxIcon("app_icon"));
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");
320 wxMenuBar
*pMenu
= new wxMenuBar
;
321 pMenu
->Append(pMenuFile
, "&File");
322 pMenu
->Append(CreateRegistryMenu(), "&Registry");
325 // create child controls
326 // ---------------------
327 m_treeCtrl
= new RegTreeCtrl(this, Ctrl_RegTree
);
329 // create the status line
330 // ----------------------
334 RegFrame::~RegFrame()
336 // this makes deletion of it *much* quicker
340 void RegFrame::OnQuit(wxCommandEvent
& event
)
345 void RegFrame::OnAbout(wxCommandEvent
& event
)
347 wxMessageDialog
dialog(this,
348 "wxRegistry sample\n"
349 "© 1998, 2000 Vadim Zeitlin",
350 "About wxRegTest", wxOK
);
355 void RegFrame::OnTest(wxCommandEvent
& event
)
357 m_treeCtrl
->OnMenuTest();
360 void RegFrame::OnExpand(wxCommandEvent
& event
)
362 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_EXPAND
);
365 void RegFrame::OnCollapse(wxCommandEvent
& event
)
367 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_COLLAPSE
);
370 void RegFrame::OnToggle(wxCommandEvent
& event
)
372 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_TOGGLE
);
375 void RegFrame::OnRefresh(wxCommandEvent
& event
)
377 m_treeCtrl
->Refresh();
380 void RegFrame::OnDelete(wxCommandEvent
& event
)
382 m_treeCtrl
->DeleteSelected();
385 void RegFrame::OnNewKey(wxCommandEvent
& event
)
387 if ( m_treeCtrl
->IsKeySelected() ) {
388 m_treeCtrl
->CreateNewKey(
389 wxGetTextFromUser("Enter the name of the new key"));
393 void RegFrame::OnNewText(wxCommandEvent
& event
)
395 if ( m_treeCtrl
->IsKeySelected() ) {
396 m_treeCtrl
->CreateNewTextValue(
397 wxGetTextFromUser("Enter the name for the new text value"));
401 void RegFrame::OnNewBinary(wxCommandEvent
& event
)
403 if ( m_treeCtrl
->IsKeySelected() ) {
404 m_treeCtrl
->CreateNewBinaryValue(
405 wxGetTextFromUser("Enter the name for the new binary value"));
409 void RegFrame::OnInfo(wxCommandEvent
& event
)
411 m_treeCtrl
->ShowProperties();
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
417 RegImageList::RegImageList() : wxImageList(16, 16, TRUE
)
419 // should be in sync with enum RegImageList::RegIcon
420 static const char *aszIcons
[] = { "key1","key2","key3","value1","value2" };
421 wxString str
= "icon_";
422 for ( unsigned int n
= 0; n
< WXSIZEOF(aszIcons
); n
++ ) {
423 Add(wxIcon(str
+ aszIcons
[n
], wxBITMAP_TYPE_ICO_RESOURCE
));
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 // create a new tree item and insert it into the tree
432 RegTreeCtrl::TreeNode
*RegTreeCtrl::InsertNewTreeNode(TreeNode
*pParent
,
433 const wxString
& strName
,
435 const wxString
*pstrValue
)
437 // create new item & insert it
438 TreeNode
*pNewNode
= new TreeNode
;
439 pNewNode
->m_pTree
= this;
440 pNewNode
->m_pParent
= pParent
;
441 pNewNode
->m_strName
= strName
;
442 pNewNode
->m_bKey
= pstrValue
== NULL
;
443 pNewNode
->m_pKey
= NULL
;
444 pNewNode
->m_id
= InsertItem(pParent
? pParent
->Id() : 0,
445 pNewNode
->IsKey() ? strName
: *pstrValue
,
448 wxASSERT_MSG( pNewNode
->m_id
, "can't create tree control item!");
450 // save the pointer in the item
451 SetItemData(pNewNode
->m_id
, pNewNode
);
453 // add it to the list of parent's children
454 if ( pParent
!= NULL
) {
455 pParent
->m_aChildren
.Add(pNewNode
);
458 if ( pNewNode
->IsKey() ) {
459 SetItemHasChildren(pNewNode
->Id());
461 if ( !pNewNode
->IsRoot() ) {
462 // set the expanded icon as well
463 SetItemImage(pNewNode
->Id(),
464 RegImageList::OpenedKey
,
465 wxTreeItemIcon_Expanded
);
472 RegTreeCtrl::RegTreeCtrl(wxWindow
*parent
, wxWindowID id
)
473 : wxTreeCtrl(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
474 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
)
477 m_draggedItem
= NULL
;
478 m_restoreStatus
= FALSE
;
480 // create the image list
481 // ---------------------
482 m_imageList
= new RegImageList
;
483 SetImageList(m_imageList
, wxIMAGE_LIST_NORMAL
);
487 m_pRoot
= InsertNewTreeNode(NULL
, "Registry Root", RegImageList::Root
);
491 m_pMenuPopup
= CreateRegistryMenu();
494 RegTreeCtrl::~RegTreeCtrl()
497 // delete m_pRoot; -- this is done by the tree now
501 void RegTreeCtrl::AddStdKeys()
503 for ( unsigned int ui
= 0; ui
< wxRegKey::nStdKeys
; ui
++ ) {
504 InsertNewTreeNode(m_pRoot
, wxRegKey::GetStdKeyName(ui
));
508 // ----------------------------------------------------------------------------
510 // ----------------------------------------------------------------------------
512 void RegTreeCtrl::OnIdle(wxIdleEvent
& WXUNUSED(event
))
514 if ( m_restoreStatus
) {
515 // restore it after OnItemExpanding()
517 wxSetCursor(*wxSTANDARD_CURSOR
);
519 m_restoreStatus
= FALSE
;
523 void RegTreeCtrl::OnRightClick(wxMouseEvent
& event
)
526 long lId
= HitTest(wxPoint(event
.GetX(), event
.GetY()), iFlags
);
527 if ( iFlags
& wxTREE_HITTEST_ONITEMLABEL
) {
528 // select the item first
531 //else: take the currently selected item if click not on item
533 PopupMenu(m_pMenuPopup
, event
.GetX(), event
.GetY());
537 void RegTreeCtrl::OnDeleteItem(wxTreeEvent
& event
)
541 // test the key creation functions
542 void RegTreeCtrl::OnMenuTest()
544 long lId
= GetSelection();
545 TreeNode
*pNode
= (TreeNode
*)GetItemData(lId
);
547 wxCHECK_RET( pNode
!= NULL
, "tree item without data?" );
549 if ( pNode
->IsRoot() ) {
550 wxLogError("Can't create a subkey under the root key.");
553 if ( !pNode
->IsKey() ) {
554 wxLogError("Can't create a subkey under a value!");
558 wxRegKey
key1(pNode
->Key(), "key1");
559 if ( key1
.Create() ) {
560 wxRegKey
key2a(key1
, "key2a"), key2b(key1
, "key2b");
561 if ( key2a
.Create() && key2b
.Create() ) {
562 // put some values under the newly created keys
563 key1
.SetValue("first_term", "10");
564 key1
.SetValue("second_term", "7");
565 key2a
= "this is the unnamed value";
566 key2b
.SetValue("sum", 17);
570 wxLogStatus("Test keys successfully added.");
575 wxLogError("Creation of test keys failed.");
578 void RegTreeCtrl::OnChar(wxKeyEvent
& event
)
580 switch ( event
.KeyCode() )
587 if ( event
.AltDown() )
598 void RegTreeCtrl::OnSelChanged(wxTreeEvent
& event
)
600 wxFrame
*pFrame
= (wxFrame
*)(wxWindow::GetParent());
601 pFrame
->SetStatusText(GetNode(event
)->FullName(), 1);
604 void RegTreeCtrl::OnItemExpanding(wxTreeEvent
& event
)
606 TreeNode
*pNode
= GetNode(event
);
607 bool bExpanding
= event
.GetCode() == wxTREE_EXPAND_EXPAND
;
609 // expansion might take some time
610 wxSetCursor(*wxHOURGLASS_CURSOR
);
611 wxLogStatus("Working...");
612 wxYield(); // to give the status line a chance to refresh itself
613 m_restoreStatus
= TRUE
; // some time later...
615 if ( pNode
->IsKey() ) {
617 // expanding: add subkeys/values
618 if ( !pNode
->OnExpand() )
622 // collapsing: clean up
628 void RegTreeCtrl::OnBeginDrag(wxTreeEvent
& event
)
630 m_copyOnDrop
= event
.GetEventType() == wxEVT_COMMAND_TREE_BEGIN_DRAG
;
632 TreeNode
*pNode
= GetNode(event
);
633 if ( pNode
->IsRoot() || pNode
->Parent()->IsRoot() )
635 wxLogStatus("This registry key can't be %s.",
636 m_copyOnDrop
? "copied" : "moved");
640 wxLogStatus("%s item %s...",
641 m_copyOnDrop
? "Copying" : "Moving",
644 m_draggedItem
= pNode
;
650 void RegTreeCtrl::OnEndDrag(wxTreeEvent
& event
)
652 wxCHECK_RET( m_draggedItem
, "end drag without begin drag?" );
654 // clear the pointer anyhow
655 TreeNode
*src
= m_draggedItem
;
656 m_draggedItem
= NULL
;
658 // where are we going to drop it?
659 TreeNode
*dst
= GetNode(event
);
660 if ( dst
&& !dst
->IsKey() ) {
661 // we need a parent key
664 if ( !dst
|| dst
->IsRoot() ) {
665 wxLogError("Can't create a key here.");
670 bool isKey
= src
->IsKey();
671 if ( (isKey
&& (src
== dst
)) ||
672 (!isKey
&& (dst
->Parent() == src
)) ) {
673 wxLogStatus("Can't copy something on itself");
678 // remove the "Registry Root\\" from the full name
679 wxString nameSrc
, nameDst
;
680 nameSrc
<< wxString(src
->FullName()).AfterFirst('\\');
681 nameDst
<< wxString(dst
->FullName()).AfterFirst('\\') << '\\'
682 << wxString(src
->FullName()).AfterLast('\\');
684 wxString verb
= m_copyOnDrop
? "copy" : "move";
685 wxString what
= isKey
? "key" : "value";
687 if ( wxMessageBox(wxString::Format
689 "Do you really want to %s the %s %s to %s?",
696 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
, this) != wxYES
) {
700 bool dstExpanded
= IsExpanded(dst
->Id());
704 wxRegKey
& key
= src
->Key();
705 wxRegKey
keyDst(dst
->Key(), src
->m_strName
);
706 ok
= keyDst
.Create(FALSE
);
708 wxLogError("Key '%s' already exists");
711 ok
= key
.Copy(keyDst
);
713 if ( ok
&& dstExpanded
) {
718 if ( ok
&& !m_copyOnDrop
) {
719 // delete the old key
720 ok
= key
.DeleteSelf();
722 src
->Parent()->Refresh();
727 wxRegKey
& key
= src
->Parent()->Key();
728 ok
= key
.CopyValue(src
->m_strName
, dst
->Key());
729 if ( ok
&& !m_copyOnDrop
) {
730 // we moved it, so delete the old one
731 ok
= key
.DeleteValue(src
->m_strName
);
740 wxLogError("Failed to %s registry %s.", verb
.c_str(), what
.c_str());
744 // ----------------------------------------------------------------------------
745 // TreeNode implementation
746 // ----------------------------------------------------------------------------
747 bool RegTreeCtrl::TreeNode::OnExpand()
749 // we add children only once
750 if ( !m_aChildren
.IsEmpty() ) {
751 // we've been already expanded
756 // we're the root key
757 m_pTree
->AddStdKeys();
761 if ( Parent()->IsRoot() ) {
762 // we're a standard key
763 m_pKey
= new wxRegKey(m_strName
);
766 // we're a normal key
767 m_pKey
= new wxRegKey(*(Parent()->m_pKey
), m_strName
);
770 if ( !m_pKey
->Open() ) {
771 wxLogError("The key '%s' can't be opened.", FullName());
775 // if we're empty, we shouldn't be expandable at all
778 // enumeration variables
783 // enumerate all subkeys
784 bCont
= m_pKey
->GetFirstKey(str
, l
);
786 m_pTree
->InsertNewTreeNode(this, str
, RegImageList::ClosedKey
);
787 bCont
= m_pKey
->GetNextKey(str
, l
);
789 // we have at least this key...
793 // enumerate all values
794 bCont
= m_pKey
->GetFirstValue(str
, l
);
798 strItem
= "<default>";
803 // determine the appropriate icon
804 RegImageList::Icon icon
;
805 switch ( m_pKey
->GetValueType(str
) ) {
806 case wxRegKey::Type_String
:
807 case wxRegKey::Type_Expand_String
:
808 case wxRegKey::Type_Multi_String
:
811 icon
= RegImageList::TextValue
;
812 m_pKey
->QueryValue(str
, strValue
);
817 case wxRegKey::Type_None
:
818 // @@ handle the error...
819 icon
= RegImageList::BinaryValue
;
822 case wxRegKey::Type_Dword
:
825 m_pKey
->QueryValue(str
, &l
);
832 icon
= RegImageList::BinaryValue
;
835 m_pTree
->InsertNewTreeNode(this, str
, icon
, &strItem
);
836 bCont
= m_pKey
->GetNextValue(str
, l
);
838 // we have at least this value...
843 // this is for the case when our last child was just deleted
844 m_pTree
->Collapse(Id());
846 // we won't be expanded any more
847 m_pTree
->SetItemHasChildren(Id(), FALSE
);
853 void RegTreeCtrl::TreeNode::OnCollapse()
861 void RegTreeCtrl::TreeNode::Refresh()
866 if ( m_pTree
->IsExpanded(Id()) ) {
867 m_pTree
->Collapse(Id());
869 m_pTree
->SetItemHasChildren(Id());
870 m_pTree
->Expand(Id());
874 // just allow it to be expanded
875 m_pTree
->SetItemHasChildren(Id());
879 bool RegTreeCtrl::TreeNode::DeleteChild(TreeNode
*child
)
881 int index
= m_aChildren
.Index(child
);
882 wxCHECK_MSG( index
!= wxNOT_FOUND
, FALSE
,
883 "our child in tree should be in m_aChildren" );
885 m_aChildren
.RemoveAt((size_t)index
);
888 if ( child
->IsKey() ) {
889 // must close key before deleting it
892 ok
= Key().DeleteKey(child
->m_strName
);
895 ok
= Key().DeleteValue(child
->m_strName
);
899 m_pTree
->Delete(child
->Id());
907 void RegTreeCtrl::TreeNode::DestroyChildren()
909 // destroy all children
910 size_t nCount
= m_aChildren
.GetCount();
911 for ( size_t n
= 0; n
< nCount
; n
++ ) {
912 long lId
= m_aChildren
[n
]->Id();
913 // no, wxTreeCtrl will do it
914 //delete m_aChildren[n];
915 m_pTree
->Delete(lId
);
921 RegTreeCtrl::TreeNode::~TreeNode()
926 const char *RegTreeCtrl::TreeNode::FullName() const
928 static wxString s_strName
;
931 return "Registry Root";
934 // our own registry key might not (yet) exist or we might be a value,
935 // so just use the parent's and concatenate
936 s_strName
= Parent()->FullName();
937 s_strName
<< '\\' << m_strName
;
943 // ----------------------------------------------------------------------------
944 // operations on RegTreeCtrl
945 // ----------------------------------------------------------------------------
947 void RegTreeCtrl::DeleteSelected()
949 long lCurrent
= GetSelection(),
950 lParent
= GetParent(lCurrent
);
952 if ( lParent
== 0 ) {
953 wxLogError("Can't delete root key.");
957 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
),
958 *pParent
= (TreeNode
*)GetItemData(lParent
);
960 wxCHECK_RET( pCurrent
&& pParent
, "either node or parent without data?" );
962 if ( pParent
->IsRoot() ) {
963 wxLogError("Can't delete standard key.");
967 wxString what
= pCurrent
->IsKey() ? "key" : "value";
968 if ( wxMessageBox(wxString::Format
970 "Do you really want to delete this %s?",
974 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
, this) != wxYES
) {
978 pParent
->DeleteChild(pCurrent
);
981 void RegTreeCtrl::CreateNewKey(const wxString
& strName
)
983 long lCurrent
= GetSelection();
984 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
986 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
988 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
990 if ( pCurrent
->IsRoot() ) {
991 wxLogError("Can't create a new key under the root key.");
995 wxRegKey
key(pCurrent
->Key(), strName
);
1000 void RegTreeCtrl::CreateNewTextValue(const wxString
& strName
)
1002 long lCurrent
= GetSelection();
1003 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
1005 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
1007 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
1009 if ( pCurrent
->IsRoot() ) {
1010 wxLogError("Can't create a new value under the root key.");
1014 if ( pCurrent
->Key().SetValue(strName
, "") )
1015 pCurrent
->Refresh();
1018 void RegTreeCtrl::CreateNewBinaryValue(const wxString
& strName
)
1020 long lCurrent
= GetSelection();
1021 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
1023 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
1025 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
1027 if ( pCurrent
->IsRoot() ) {
1028 wxLogError("Can't create a new value under the root key.");
1032 if ( pCurrent
->Key().SetValue(strName
, 0) )
1033 pCurrent
->Refresh();
1036 void RegTreeCtrl::ShowProperties()
1038 long lCurrent
= GetSelection();
1039 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
1041 if ( !pCurrent
|| pCurrent
->IsRoot() )
1043 wxLogStatus("No properties");
1048 if ( pCurrent
->IsKey() )
1050 const wxRegKey
& key
= pCurrent
->Key();
1051 size_t nSubKeys
, nValues
;
1052 if ( !key
.GetKeyInfo(&nSubKeys
, NULL
, &nValues
, NULL
) )
1054 wxLogError("Couldn't get key info");
1058 wxLogMessage("Key '%s' has %u subkeys and %u values.",
1059 key
.GetName().c_str(), nSubKeys
, nValues
);
1062 else // it's a value
1064 TreeNode
*parent
= pCurrent
->Parent();
1065 wxCHECK_RET( parent
, "reg value without key?" );
1067 const wxRegKey
& key
= parent
->Key();
1068 const char *value
= pCurrent
->m_strName
.c_str();
1069 wxLogMessage("Value '%s' under the key '%s' is of type "
1072 parent
->m_strName
.c_str(),
1073 key
.GetValueType(value
),
1074 key
.IsNumericValue(value
) ? "numeric" : "string");
1079 bool RegTreeCtrl::IsKeySelected() const
1081 long lCurrent
= GetSelection();
1082 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
1084 wxCHECK( pCurrent
!= NULL
, FALSE
);
1086 return pCurrent
->IsKey();
1089 void RegTreeCtrl::Refresh()
1091 long lId
= GetSelection();
1095 TreeNode
*pNode
= (TreeNode
*)GetItemData(lId
);
1097 wxCHECK_RET( pNode
!= NULL
, "tree item without data?" );