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"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
36 class RegApp
: public wxApp
42 // ----------------------------------------------------------------------------
43 // image list with registry icons
44 // ----------------------------------------------------------------------------
45 class RegImageList
: public wxImageList
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
63 class RegTreeCtrl
: public wxTreeCtrl
67 RegTreeCtrl(wxWindow
*parent
, wxWindowID id
);
68 virtual ~RegTreeCtrl();
71 void OnDeleteItem (wxTreeEvent
& event
);
72 void OnItemExpanding(wxTreeEvent
& event
);
73 void OnSelChanged (wxTreeEvent
& event
);
75 void OnRightClick (wxMouseEvent
& event
);
76 void OnChar (wxKeyEvent
& event
);
78 // forwarded notifications (by the frame)
82 void DeleteSelected();
83 void CreateNewKey(const wxString
& strName
);
84 void CreateNewTextValue(const wxString
& strName
);
85 void CreateNewBinaryValue(const wxString
& strName
);
88 bool IsKeySelected() const;
90 DECLARE_EVENT_TABLE();
93 // array of children of the node
95 WX_DEFINE_ARRAY(TreeNode
*, TreeChildren
);
97 // structure describing a registry key/value
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)
110 TreeNode() { m_lDummy
= 0; }
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
; }
123 void Refresh() { OnCollapse(); OnExpand(); }
125 void DestroyChildren();
126 const char *FullName() const;
128 // get the associated key: make sure the pointer is !NULL
129 wxRegKey
& Key() { if ( !m_pKey
) OnExpand(); return *m_pKey
; }
131 // dtor deletes all children
135 wxMenu
*m_pMenuPopup
;
137 wxImageList
*m_imageList
;
139 TreeNode
*GetNode(const wxTreeEvent
& event
)
140 { return (TreeNode
*)GetItemData(event
.m_item
.m_itemId
); }
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
152 // ----------------------------------------------------------------------------
153 // the main window of our application
154 // ----------------------------------------------------------------------------
155 class RegFrame
: public wxFrame
159 RegFrame(wxFrame
*parent
, char *title
, int x
, int y
, int w
, int h
);
160 virtual ~RegFrame(void);
163 void OnQuit (wxCommandEvent
& event
);
164 void OnAbout(wxCommandEvent
& event
);
165 void OnTest (wxCommandEvent
& event
);
167 void OnExpand (wxCommandEvent
& event
);
168 void OnCollapse(wxCommandEvent
& event
);
169 void OnToggle (wxCommandEvent
& event
);
171 void OnDelete (wxCommandEvent
& event
);
172 void OnNewKey (wxCommandEvent
& event
);
173 void OnNewText (wxCommandEvent
& event
);
174 void OnNewBinary(wxCommandEvent
& event
);
176 bool OnClose () { return TRUE
; }
178 DECLARE_EVENT_TABLE();
181 RegTreeCtrl
*m_treeCtrl
;
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
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
)
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
)
227 EVT_CHAR (RegTreeCtrl::OnChar
)
228 EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick
)
231 // ============================================================================
233 // ============================================================================
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 // create the "registry operations" menu
240 wxMenu
*CreateRegistryMenu()
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");
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");
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
262 IMPLEMENT_APP(RegApp
)
264 // `Main program' equivalent, creating windows and returning main app frame
265 bool RegApp::OnInit()
267 // create the main frame window and show it
268 RegFrame
*frame
= new RegFrame(NULL
, "wxRegKey Test", 50, 50, 600, 350);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
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
))
283 // this reduces flicker effects
284 SetBackgroundColour(wxColour(255, 255, 255));
288 SetIcon(wxIcon("app_icon"));
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");
299 wxMenuBar
*pMenu
= new wxMenuBar
;
300 pMenu
->Append(pMenuFile
, "&File");
301 pMenu
->Append(CreateRegistryMenu(), "&Registry");
304 // create child controls
305 // ---------------------
306 m_treeCtrl
= new RegTreeCtrl(this, Ctrl_RegTree
);
308 // create the status line
309 // ----------------------
314 SetStatusWidths(2, aWidths
);
317 RegFrame::~RegFrame(void)
321 void RegFrame::OnQuit(wxCommandEvent
& event
)
326 void RegFrame::OnAbout(wxCommandEvent
& event
)
328 wxMessageDialog
dialog(this, "wxRegistry sample\n(c) 1998 Vadim Zeitlin",
329 "About wxRegistry", wxOK
);
334 void RegFrame::OnTest(wxCommandEvent
& event
)
336 m_treeCtrl
->OnMenuTest();
339 void RegFrame::OnExpand(wxCommandEvent
& event
)
341 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_EXPAND
);
344 void RegFrame::OnCollapse(wxCommandEvent
& event
)
346 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_COLLAPSE
);
349 void RegFrame::OnToggle(wxCommandEvent
& event
)
351 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_TOGGLE
);
354 void RegFrame::OnDelete(wxCommandEvent
& event
)
356 m_treeCtrl
->DeleteSelected();
359 void RegFrame::OnNewKey(wxCommandEvent
& event
)
361 if ( m_treeCtrl
->IsKeySelected() ) {
362 m_treeCtrl
->CreateNewKey(
363 wxGetTextFromUser("Enter the name of the new key"));
367 void RegFrame::OnNewText(wxCommandEvent
& event
)
369 if ( m_treeCtrl
->IsKeySelected() ) {
370 m_treeCtrl
->CreateNewTextValue(
371 wxGetTextFromUser("Enter the name for the new text value"));
375 void RegFrame::OnNewBinary(wxCommandEvent
& event
)
377 if ( m_treeCtrl
->IsKeySelected() ) {
378 m_treeCtrl
->CreateNewBinaryValue(
379 wxGetTextFromUser("Enter the name for the new binary value"));
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
386 RegImageList::RegImageList() : wxImageList(16, 16, TRUE
)
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
));
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 // create a new tree item and insert it into the tree
401 RegTreeCtrl::TreeNode
*RegTreeCtrl::InsertNewTreeNode(TreeNode
*pParent
,
402 const wxString
& strName
,
404 const wxString
*pstrValue
)
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
,
417 wxASSERT_MSG( pNewNode
->m_id
, "can't create tree control item!");
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!");
424 // add it to the list of parent's children
425 if ( pParent
!= NULL
) {
426 pParent
->m_aChildren
.Add(pNewNode
);
429 // force the [+] button (@@@ not very elegant...)
430 if ( pNewNode
->IsKey() )
431 pNewNode
->AddDummy();
436 RegTreeCtrl::RegTreeCtrl(wxWindow
*parent
, wxWindowID id
)
437 : wxTreeCtrl(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
438 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
)
440 // create the image list
441 // ---------------------
442 m_imageList
= new RegImageList
;
443 SetImageList(m_imageList
, wxIMAGE_LIST_NORMAL
);
447 m_pRoot
= InsertNewTreeNode(NULL
, "Registry Root", RegImageList::Root
);
451 m_pMenuPopup
= CreateRegistryMenu();
454 RegTreeCtrl::~RegTreeCtrl()
461 void RegTreeCtrl::AddStdKeys()
463 for ( uint ui
= 0; ui
< wxRegKey::nStdKeys
; ui
++ ) {
464 InsertNewTreeNode(m_pRoot
, wxRegKey::GetStdKeyName(ui
));
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 void RegTreeCtrl::OnRightClick(wxMouseEvent
& event
)
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 );
480 PopupMenu(m_pMenuPopup
, event
.GetX(), event
.GetY());
485 void RegTreeCtrl::OnDeleteItem(wxTreeEvent
& event
)
489 // test the key creation functions
490 void RegTreeCtrl::OnMenuTest()
492 long lId
= GetSelection();
493 TreeNode
*pNode
= (TreeNode
*)GetItemData(lId
);
495 wxCHECK_RET( pNode
!= NULL
, "tree item without data?" );
497 if ( pNode
->IsRoot() ) {
498 wxLogError("Can't create a subkey under the root key.");
501 if ( !pNode
->IsKey() ) {
502 wxLogError("Can't create a subkey under a value!");
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);
518 wxLogStatus("Test keys successfully added.");
523 wxLogError("Creation of test keys failed.");
526 void RegTreeCtrl::OnChar(wxKeyEvent
& event
)
528 if ( event
.KeyCode() == WXK_DELETE
)
531 wxTreeCtrl::OnChar(event
);
534 void RegTreeCtrl::OnSelChanged(wxTreeEvent
& event
)
536 wxFrame
*pFrame
= (wxFrame
*)(wxWindow::GetParent());
537 pFrame
->SetStatusText(GetNode(event
)->FullName(), 1);
540 void RegTreeCtrl::OnItemExpanding(wxTreeEvent
& event
)
542 TreeNode
*pNode
= GetNode(event
);
543 bool bExpanding
= event
.m_code
== wxTREE_EXPAND_EXPAND
;
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
550 if ( pNode
->IsKey() ) {
552 // expanding: add subkeys/values
553 if ( !pNode
->OnExpand() )
557 // collapsing: clean up
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
);
570 wxSetCursor(*wxSTANDARD_CURSOR
);
573 // ----------------------------------------------------------------------------
574 // TreeNode implementation
575 // ----------------------------------------------------------------------------
576 bool RegTreeCtrl::TreeNode::OnExpand()
580 m_pTree
->DeleteItem(m_lDummy
);
583 // we're the root key
584 m_pTree
->AddStdKeys();
588 if ( Parent()->IsRoot() ) {
589 // we're a standard key
590 m_pKey
= new wxRegKey(m_strName
);
593 // we're a normal key
594 m_pKey
= new wxRegKey(*(Parent()->m_pKey
), m_strName
);
597 if ( !m_pKey
->Open() ) {
599 wxLogError("The key '%s' can't be opened.", FullName());
603 // enumeration variables
608 // enumerate all subkeys
609 bCont
= m_pKey
->GetFirstKey(str
, l
);
611 m_pTree
->InsertNewTreeNode(this, str
, RegImageList::ClosedKey
);
612 bCont
= m_pKey
->GetNextKey(str
, l
);
615 // enumerate all values
616 bCont
= m_pKey
->GetFirstValue(str
, l
);
620 strItem
= "<default>";
625 // determine the appropriate icon
626 RegImageList::Icon icon
;
627 switch ( m_pKey
->GetValueType(str
) ) {
628 case wxRegKey::Type_String
:
629 case wxRegKey::Type_Expand_String
:
630 case wxRegKey::Type_Multi_String
:
633 icon
= RegImageList::TextValue
;
634 m_pKey
->QueryValue(str
, strValue
);
639 case wxRegKey::Type_None
:
640 // @@ handle the error...
641 icon
= RegImageList::BinaryValue
;
644 case wxRegKey::Type_Dword
:
648 m_pKey
->QueryValue(str
, &l
);
649 sprintf(szBuf
, "%lx", l
);
656 icon
= RegImageList::BinaryValue
;
659 m_pTree
->InsertNewTreeNode(this, str
, icon
, &strItem
);
660 bCont
= m_pKey
->GetNextValue(str
, l
);
666 void RegTreeCtrl::TreeNode::OnCollapse()
668 bool bHasChildren
= !m_aChildren
.IsEmpty();
679 void RegTreeCtrl::TreeNode::AddDummy()
681 // insert dummy item forcing appearance of [+] button
682 m_lDummy
= m_pTree
->InsertItem(Id(), "");
685 void RegTreeCtrl::TreeNode::DestroyChildren()
687 // destroy all children
688 uint nCount
= m_aChildren
.Count();
689 for ( uint n
= 0; n
< nCount
; n
++ ) {
690 long lId
= m_aChildren
[n
]->Id();
691 delete m_aChildren
[n
];
692 m_pTree
->DeleteItem(lId
);
698 RegTreeCtrl::TreeNode::~TreeNode()
705 const char *RegTreeCtrl::TreeNode::FullName() const
707 static wxString s_strName
;
710 return "Registry Root";
713 // our own registry key might not (yet) exist or we might be a value,
714 // so just use the parent's and concatenate
715 s_strName
= Parent()->FullName();
716 s_strName
<< '\\' << m_strName
;
722 // ----------------------------------------------------------------------------
723 // operations on RegTreeCtrl
724 // ----------------------------------------------------------------------------
726 void RegTreeCtrl::DeleteSelected()
728 long lCurrent
= GetSelection(),
729 lParent
= GetParent(lCurrent
);
731 if ( lParent
== 0 ) {
732 wxLogError("Can't delete root key.");
736 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
),
737 *pParent
= (TreeNode
*)GetItemData(lParent
);
739 wxCHECK_RET( pCurrent
&& pParent
, "either node or parent without data?" );
741 if ( pParent
->IsRoot() ) {
742 wxLogError("Can't delete standard key.");
746 if ( pCurrent
->IsKey() ) {
747 if ( wxMessageBox("Do you really want to delete this key?",
749 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
, this) != wxYES
) {
753 // must close key before deleting it
754 pCurrent
->OnCollapse();
756 if ( pParent
->Key().DeleteKey(pCurrent
->m_strName
) )
760 if ( wxMessageBox("Do you really want to delete this value?",
762 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
, this) != wxYES
) {
766 if ( pParent
->Key().DeleteValue(pCurrent
->m_strName
) )
771 void RegTreeCtrl::CreateNewKey(const wxString
& strName
)
773 long lCurrent
= GetSelection();
774 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
776 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
778 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
780 if ( pCurrent
->IsRoot() ) {
781 wxLogError("Can't create a new key under the root key.");
785 wxRegKey
key(pCurrent
->Key(), strName
);
790 void RegTreeCtrl::CreateNewTextValue(const wxString
& strName
)
792 long lCurrent
= GetSelection();
793 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
795 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
797 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
799 if ( pCurrent
->IsRoot() ) {
800 wxLogError("Can't create a new value under the root key.");
804 if ( pCurrent
->Key().SetValue(strName
, "") )
808 void RegTreeCtrl::CreateNewBinaryValue(const wxString
& strName
)
810 long lCurrent
= GetSelection();
811 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
813 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
815 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
817 if ( pCurrent
->IsRoot() ) {
818 wxLogError("Can't create a new value under the root key.");
822 if ( pCurrent
->Key().SetValue(strName
, 0) )
826 bool RegTreeCtrl::IsKeySelected() const
828 long lCurrent
= GetSelection();
829 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
831 wxCHECK( pCurrent
!= NULL
, false );
833 return pCurrent
->IsKey();