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 // array of children of the node
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
67 class RegTreeCtrl
: public wxTreeCtrl
71 RegTreeCtrl(wxWindow
*parent
, wxWindowID id
);
72 virtual ~RegTreeCtrl();
75 void OnDeleteItem (wxTreeEvent
& event
);
76 void OnItemExpanding(wxTreeEvent
& event
);
77 void OnSelChanged (wxTreeEvent
& event
);
79 void OnRightClick (wxMouseEvent
& event
);
80 void OnChar (wxKeyEvent
& event
);
82 // forwarded notifications (by the frame)
86 void DeleteSelected();
87 void CreateNewKey(const wxString
& strName
);
88 void CreateNewTextValue(const wxString
& strName
);
89 void CreateNewBinaryValue(const wxString
& strName
);
92 bool IsKeySelected() const;
94 DECLARE_EVENT_TABLE();
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
110 long m_lDummy
; // dummy subkey (to make expansion possible)
113 TreeNode() { m_lDummy
= 0; }
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
; }
126 void Refresh() { OnCollapse(); OnExpand(); }
128 void DestroyChildren();
129 const char *FullName() const;
131 // get the associated key: make sure the pointer is !NULL
132 wxRegKey
& Key() { if ( !m_pKey
) OnExpand(); return *m_pKey
; }
134 // dtor deletes all children
138 wxMenu
*m_pMenuPopup
;
140 wxImageList
*m_imageList
;
142 TreeNode
*GetNode(const wxTreeEvent
& event
)
143 { return (TreeNode
*)GetItemData((WXHTREEITEM
)event
.GetItem()); }
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
155 // ----------------------------------------------------------------------------
156 // the main window of our application
157 // ----------------------------------------------------------------------------
158 class RegFrame
: public wxFrame
162 RegFrame(wxFrame
*parent
, char *title
, int x
, int y
, int w
, int h
);
163 virtual ~RegFrame(void);
166 void OnQuit (wxCommandEvent
& event
);
167 void OnAbout(wxCommandEvent
& event
);
168 void OnTest (wxCommandEvent
& event
);
170 void OnExpand (wxCommandEvent
& event
);
171 void OnCollapse(wxCommandEvent
& event
);
172 void OnToggle (wxCommandEvent
& event
);
174 void OnDelete (wxCommandEvent
& event
);
175 void OnNewKey (wxCommandEvent
& event
);
176 void OnNewText (wxCommandEvent
& event
);
177 void OnNewBinary(wxCommandEvent
& event
);
179 bool OnClose () { return TRUE
; }
181 DECLARE_EVENT_TABLE();
184 RegTreeCtrl
*m_treeCtrl
;
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
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
)
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
)
230 EVT_CHAR (RegTreeCtrl::OnChar
)
231 EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick
)
234 // ============================================================================
236 // ============================================================================
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 // create the "registry operations" menu
243 wxMenu
*CreateRegistryMenu()
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");
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");
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
265 IMPLEMENT_APP(RegApp
)
267 // `Main program' equivalent, creating windows and returning main app frame
268 bool RegApp::OnInit()
270 // create the main frame window and show it
271 RegFrame
*frame
= new RegFrame(NULL
, "wxRegKey Test", 50, 50, 600, 350);
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
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
))
286 // this reduces flicker effects
287 SetBackgroundColour(wxColour(255, 255, 255));
291 SetIcon(wxIcon("app_icon"));
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");
302 wxMenuBar
*pMenu
= new wxMenuBar
;
303 pMenu
->Append(pMenuFile
, "&File");
304 pMenu
->Append(CreateRegistryMenu(), "&Registry");
307 // create child controls
308 // ---------------------
309 m_treeCtrl
= new RegTreeCtrl(this, Ctrl_RegTree
);
311 // create the status line
312 // ----------------------
317 SetStatusWidths(2, aWidths
);
320 RegFrame::~RegFrame(void)
324 void RegFrame::OnQuit(wxCommandEvent
& event
)
329 void RegFrame::OnAbout(wxCommandEvent
& event
)
331 wxMessageDialog
dialog(this, "wxRegistry sample\n(c) 1998 Vadim Zeitlin",
332 "About wxRegistry", wxOK
);
337 void RegFrame::OnTest(wxCommandEvent
& event
)
339 m_treeCtrl
->OnMenuTest();
342 void RegFrame::OnExpand(wxCommandEvent
& event
)
344 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_EXPAND
);
347 void RegFrame::OnCollapse(wxCommandEvent
& event
)
349 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_COLLAPSE
);
352 void RegFrame::OnToggle(wxCommandEvent
& event
)
354 m_treeCtrl
->ExpandItem(m_treeCtrl
->GetSelection(), wxTREE_EXPAND_TOGGLE
);
357 void RegFrame::OnDelete(wxCommandEvent
& event
)
359 m_treeCtrl
->DeleteSelected();
362 void RegFrame::OnNewKey(wxCommandEvent
& event
)
364 if ( m_treeCtrl
->IsKeySelected() ) {
365 m_treeCtrl
->CreateNewKey(
366 wxGetTextFromUser("Enter the name of the new key"));
370 void RegFrame::OnNewText(wxCommandEvent
& event
)
372 if ( m_treeCtrl
->IsKeySelected() ) {
373 m_treeCtrl
->CreateNewTextValue(
374 wxGetTextFromUser("Enter the name for the new text value"));
378 void RegFrame::OnNewBinary(wxCommandEvent
& event
)
380 if ( m_treeCtrl
->IsKeySelected() ) {
381 m_treeCtrl
->CreateNewBinaryValue(
382 wxGetTextFromUser("Enter the name for the new binary value"));
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
389 RegImageList::RegImageList() : wxImageList(16, 16, TRUE
)
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
));
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 // create a new tree item and insert it into the tree
404 RegTreeCtrl::TreeNode
*RegTreeCtrl::InsertNewTreeNode(TreeNode
*pParent
,
405 const wxString
& strName
,
407 const wxString
*pstrValue
)
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
,
420 wxASSERT_MSG( pNewNode
->m_id
, "can't create tree control item!");
422 // save the pointer in the item
423 SetItemData(pNewNode
->m_id
, pNewNode
);
425 // add it to the list of parent's children
426 if ( pParent
!= NULL
) {
427 pParent
->m_aChildren
.Add(pNewNode
);
430 // force the [+] button (@@@ not very elegant...)
431 if ( pNewNode
->IsKey() )
432 pNewNode
->AddDummy();
437 RegTreeCtrl::RegTreeCtrl(wxWindow
*parent
, wxWindowID id
)
438 : wxTreeCtrl(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
439 wxTR_HAS_BUTTONS
| wxSUNKEN_BORDER
)
441 // create the image list
442 // ---------------------
443 m_imageList
= new RegImageList
;
444 SetImageList(m_imageList
, wxIMAGE_LIST_NORMAL
);
448 m_pRoot
= InsertNewTreeNode(NULL
, "Registry Root", RegImageList::Root
);
452 m_pMenuPopup
= CreateRegistryMenu();
455 RegTreeCtrl::~RegTreeCtrl()
462 void RegTreeCtrl::AddStdKeys()
464 for ( unsigned int ui
= 0; ui
< wxRegKey::nStdKeys
; ui
++ ) {
465 InsertNewTreeNode(m_pRoot
, wxRegKey::GetStdKeyName(ui
));
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 void RegTreeCtrl::OnRightClick(wxMouseEvent
& event
)
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 );
481 PopupMenu(m_pMenuPopup
, event
.GetX(), event
.GetY());
486 void RegTreeCtrl::OnDeleteItem(wxTreeEvent
& event
)
490 // test the key creation functions
491 void RegTreeCtrl::OnMenuTest()
493 long lId
= GetSelection();
494 TreeNode
*pNode
= (TreeNode
*)GetItemData(lId
);
496 wxCHECK_RET( pNode
!= NULL
, "tree item without data?" );
498 if ( pNode
->IsRoot() ) {
499 wxLogError("Can't create a subkey under the root key.");
502 if ( !pNode
->IsKey() ) {
503 wxLogError("Can't create a subkey under a value!");
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);
519 wxLogStatus("Test keys successfully added.");
524 wxLogError("Creation of test keys failed.");
527 void RegTreeCtrl::OnChar(wxKeyEvent
& event
)
529 if ( event
.KeyCode() == WXK_DELETE
)
535 void RegTreeCtrl::OnSelChanged(wxTreeEvent
& event
)
537 wxFrame
*pFrame
= (wxFrame
*)(wxWindow::GetParent());
538 pFrame
->SetStatusText(GetNode(event
)->FullName(), 1);
541 void RegTreeCtrl::OnItemExpanding(wxTreeEvent
& event
)
543 TreeNode
*pNode
= GetNode(event
);
544 bool bExpanding
= event
.GetCode() == wxTREE_EXPAND_EXPAND
;
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
551 if ( pNode
->IsKey() ) {
553 // expanding: add subkeys/values
554 if ( !pNode
->OnExpand() )
558 // collapsing: clean up
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
);
571 wxSetCursor(*wxSTANDARD_CURSOR
);
574 // ----------------------------------------------------------------------------
575 // TreeNode implementation
576 // ----------------------------------------------------------------------------
577 bool RegTreeCtrl::TreeNode::OnExpand()
580 if ( m_lDummy
!= 0 ) {
581 m_pTree
->Delete(m_lDummy
);
585 // we've been already expanded
590 // we're the root key
591 m_pTree
->AddStdKeys();
595 if ( Parent()->IsRoot() ) {
596 // we're a standard key
597 m_pKey
= new wxRegKey(m_strName
);
600 // we're a normal key
601 m_pKey
= new wxRegKey(*(Parent()->m_pKey
), m_strName
);
604 if ( !m_pKey
->Open() ) {
605 wxLogError("The key '%s' can't be opened.", FullName());
609 // enumeration variables
614 // enumerate all subkeys
615 bCont
= m_pKey
->GetFirstKey(str
, l
);
617 m_pTree
->InsertNewTreeNode(this, str
, RegImageList::ClosedKey
);
618 bCont
= m_pKey
->GetNextKey(str
, l
);
621 // enumerate all values
622 bCont
= m_pKey
->GetFirstValue(str
, l
);
626 strItem
= "<default>";
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
:
639 icon
= RegImageList::TextValue
;
640 m_pKey
->QueryValue(str
, strValue
);
645 case wxRegKey::Type_None
:
646 // @@ handle the error...
647 icon
= RegImageList::BinaryValue
;
650 case wxRegKey::Type_Dword
:
654 m_pKey
->QueryValue(str
, &l
);
655 sprintf(szBuf
, "%lx", l
);
662 icon
= RegImageList::BinaryValue
;
665 m_pTree
->InsertNewTreeNode(this, str
, icon
, &strItem
);
666 bCont
= m_pKey
->GetNextValue(str
, l
);
672 void RegTreeCtrl::TreeNode::OnCollapse()
674 bool bHasChildren
= !m_aChildren
.IsEmpty();
685 void RegTreeCtrl::TreeNode::AddDummy()
687 // insert dummy item forcing appearance of [+] button
688 m_lDummy
= m_pTree
->InsertItem(Id(), "");
691 void RegTreeCtrl::TreeNode::DestroyChildren()
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
);
704 RegTreeCtrl::TreeNode::~TreeNode()
711 const char *RegTreeCtrl::TreeNode::FullName() const
713 static wxString s_strName
;
716 return "Registry Root";
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
;
728 // ----------------------------------------------------------------------------
729 // operations on RegTreeCtrl
730 // ----------------------------------------------------------------------------
732 void RegTreeCtrl::DeleteSelected()
734 long lCurrent
= GetSelection(),
735 lParent
= GetParent(lCurrent
);
737 if ( lParent
== 0 ) {
738 wxLogError("Can't delete root key.");
742 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
),
743 *pParent
= (TreeNode
*)GetItemData(lParent
);
745 wxCHECK_RET( pCurrent
&& pParent
, "either node or parent without data?" );
747 if ( pParent
->IsRoot() ) {
748 wxLogError("Can't delete standard key.");
752 if ( pCurrent
->IsKey() ) {
753 if ( wxMessageBox("Do you really want to delete this key?",
755 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
, this) != wxYES
) {
759 // must close key before deleting it
760 pCurrent
->OnCollapse();
762 if ( pParent
->Key().DeleteKey(pCurrent
->m_strName
) )
766 if ( wxMessageBox("Do you really want to delete this value?",
768 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
, this) != wxYES
) {
772 if ( pParent
->Key().DeleteValue(pCurrent
->m_strName
) )
777 void RegTreeCtrl::CreateNewKey(const wxString
& strName
)
779 long lCurrent
= GetSelection();
780 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
782 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
784 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
786 if ( pCurrent
->IsRoot() ) {
787 wxLogError("Can't create a new key under the root key.");
791 wxRegKey
key(pCurrent
->Key(), strName
);
796 void RegTreeCtrl::CreateNewTextValue(const wxString
& strName
)
798 long lCurrent
= GetSelection();
799 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
801 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
803 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
805 if ( pCurrent
->IsRoot() ) {
806 wxLogError("Can't create a new value under the root key.");
810 if ( pCurrent
->Key().SetValue(strName
, "") )
814 void RegTreeCtrl::CreateNewBinaryValue(const wxString
& strName
)
816 long lCurrent
= GetSelection();
817 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
819 wxCHECK_RET( pCurrent
!= NULL
, "node without data?" );
821 wxASSERT( pCurrent
->IsKey() ); // check must have been done before
823 if ( pCurrent
->IsRoot() ) {
824 wxLogError("Can't create a new value under the root key.");
828 if ( pCurrent
->Key().SetValue(strName
, 0) )
832 bool RegTreeCtrl::IsKeySelected() const
834 long lCurrent
= GetSelection();
835 TreeNode
*pCurrent
= (TreeNode
*)GetItemData(lCurrent
);
837 wxCHECK( pCurrent
!= NULL
, false );
839 return pCurrent
->IsKey();