Remove obsolete VisualAge-related files.
[wxWidgets.git] / samples / regtest / regtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: regtest.cpp
3 // Purpose: wxRegKey class demo
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 03.04.98
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 # pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 # include "wx/wx.h"
26 #endif
27
28 #include "wx/treectrl.h"
29 #include "wx/config.h"
30 #include "wx/imaglist.h"
31 #include "wx/tokenzr.h"
32
33 #if wxUSE_CONFIG_NATIVE && defined( __WINDOWS__ )
34 # define DO_REGTEST 1
35 #else
36 # define DO_REGTEST 0
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // application type
41 // ----------------------------------------------------------------------------
42 class RegApp : public wxApp
43 {
44 public:
45 bool OnInit();
46 };
47
48 // ----------------------------------------------------------------------------
49 // image list with registry icons
50 // ----------------------------------------------------------------------------
51 class RegImageList : public wxImageList
52 {
53 public:
54 enum Icon
55 {
56 Root,
57 ClosedKey,
58 OpenedKey,
59 TextValue,
60 BinaryValue
61 };
62
63 RegImageList();
64 };
65
66 #if DO_REGTEST
67
68 // ----------------------------------------------------------------------------
69 // our control
70 // ----------------------------------------------------------------------------
71 class RegTreeCtrl : public wxTreeCtrl
72 {
73 public:
74 // ctor & dtor
75 RegTreeCtrl(wxWindow *parent, wxWindowID id);
76 virtual ~RegTreeCtrl();
77
78 // notifications
79 void OnDeleteItem (wxTreeEvent& event);
80 void OnItemExpanding (wxTreeEvent& event);
81 void OnSelChanged (wxTreeEvent& event);
82
83 void OnBeginEdit (wxTreeEvent& event);
84 void OnEndEdit (wxTreeEvent& event);
85
86 void OnBeginDrag (wxTreeEvent& event);
87 void OnEndDrag (wxTreeEvent& event);
88
89 void OnRightClick (wxMouseEvent& event);
90 void OnChar (wxKeyEvent& event);
91 void OnIdle (wxIdleEvent& event);
92
93 // forwarded notifications (by the frame)
94 void OnMenuTest();
95
96 // operations
97 void GoTo(const wxString& location);
98 void DoRefresh();
99 void DeleteSelected();
100 void ShowProperties();
101 void CreateNewKey(const wxString& strName);
102 void CreateNewTextValue(const wxString& strName);
103 void CreateNewBinaryValue(const wxString& strName);
104 void SetRegistryView(wxRegKey::WOW64ViewMode viewMode);
105
106 // information
107 bool IsKeySelected() const;
108
109 private:
110 // structure describing a registry key/value
111 class TreeNode : public wxTreeItemData
112 {
113 WX_DEFINE_ARRAY_PTR(TreeNode *, TreeChildren);
114 public:
115 RegTreeCtrl *m_pTree; // must be !NULL
116 TreeNode *m_pParent; // NULL only for the root node
117 wxTreeItemId m_id; // the id of the tree control item
118 wxString m_strName; // name of the key/value
119 TreeChildren m_aChildren; // array of subkeys/values
120 bool m_bKey; // key or value?
121 wxRegKey *m_pKey; // only may be !NULL if m_bKey == true
122 wxRegKey::WOW64ViewMode m_viewMode; // How to view the registry.
123
124 // trivial accessors
125 wxTreeItemId Id() const { return m_id; }
126 bool IsRoot() const { return m_pParent == NULL; }
127 bool IsKey() const { return m_bKey; }
128 TreeNode *Parent() const { return m_pParent; }
129
130 // notifications
131 bool OnExpand();
132 void OnCollapse();
133
134 // operations
135 void Refresh();
136 bool DeleteChild(TreeNode *child);
137 void DestroyChildren();
138 const wxChar *FullName() const;
139 void SetRegistryView(wxRegKey::WOW64ViewMode viewMode);
140
141 // get the associated key: make sure the pointer is !NULL
142 wxRegKey& Key() { if ( !m_pKey ) OnExpand(); return *m_pKey; }
143
144 // dtor deletes all children
145 ~TreeNode();
146 };
147
148 wxImageList *m_imageList;
149 wxMenu *m_pMenuPopup;
150
151 TreeNode *m_pRoot;
152
153 TreeNode *m_draggedItem; // the item being dragged
154 bool m_copyOnDrop; // if false, then move
155
156 bool m_restoreStatus; // after OnItemExpanding()
157
158 wxString m_nameOld; // the initial value of item being renamed
159
160 wxRegKey::WOW64ViewMode m_viewMode; // Registry view to use for keys.
161
162 TreeNode *GetNode(const wxTreeEvent& event)
163 { return (TreeNode *)GetItemData(event.GetItem()); }
164
165 public:
166 // create a new node and insert it to the tree
167 TreeNode *InsertNewTreeNode(TreeNode *pParent,
168 const wxString& strName,
169 int idImage = RegImageList::ClosedKey,
170 const wxString *pstrValue = NULL,
171 wxRegKey::WOW64ViewMode viewMode = wxRegKey::WOW64ViewMode_Default);
172
173 // add standard registry keys
174 void AddStdKeys();
175
176 private:
177 DECLARE_EVENT_TABLE()
178 };
179
180 #endif // #if DO_REGTEST
181
182 // ----------------------------------------------------------------------------
183 // the main window of our application
184 // ----------------------------------------------------------------------------
185 class RegFrame : public wxFrame
186 {
187 public:
188 // ctor & dtor
189 RegFrame(wxFrame *parent, const wxChar *title, int x, int y, int w, int h);
190 virtual ~RegFrame();
191
192 // callbacks
193 void OnQuit (wxCommandEvent& event);
194 void OnAbout(wxCommandEvent& event);
195 void OnTest (wxCommandEvent& event);
196
197 void OnGoTo (wxCommandEvent& event);
198
199 void OnExpand (wxCommandEvent& event);
200 void OnCollapse(wxCommandEvent& event);
201 void OnToggle (wxCommandEvent& event);
202 void OnRefresh (wxCommandEvent& event);
203
204 void OnDelete (wxCommandEvent& event);
205 void OnNewKey (wxCommandEvent& event);
206 void OnNewText (wxCommandEvent& event);
207 void OnNewBinary(wxCommandEvent& event);
208
209 void OnInfo (wxCommandEvent& event);
210
211 void OnViewChange (wxCommandEvent& event);
212
213 DECLARE_EVENT_TABLE()
214
215 private:
216
217 #if DO_REGTEST
218 RegTreeCtrl *m_treeCtrl;
219 #endif
220 };
221
222 // ----------------------------------------------------------------------------
223 // various ids
224 // ----------------------------------------------------------------------------
225
226 enum
227 {
228 Menu_Quit = 100,
229 Menu_About,
230 Menu_Test,
231 Menu_GoTo,
232 Menu_Expand,
233 Menu_Collapse,
234 Menu_Toggle,
235 Menu_Refresh,
236 Menu_New,
237 Menu_NewKey,
238 Menu_NewText,
239 Menu_NewBinary,
240 Menu_Delete,
241 Menu_Info,
242 Menu_View,
243 Menu_ViewDefault,
244 Menu_View32,
245 Menu_View64,
246
247 Ctrl_RegTree = 200
248 };
249
250 // ----------------------------------------------------------------------------
251 // event tables
252 // ----------------------------------------------------------------------------
253
254 BEGIN_EVENT_TABLE(RegFrame, wxFrame)
255 EVT_MENU(Menu_Test, RegFrame::OnTest)
256 EVT_MENU(Menu_About, RegFrame::OnAbout)
257 EVT_MENU(Menu_Quit, RegFrame::OnQuit)
258 EVT_MENU(Menu_GoTo, RegFrame::OnGoTo)
259 EVT_MENU(Menu_Expand, RegFrame::OnExpand)
260 EVT_MENU(Menu_Collapse, RegFrame::OnCollapse)
261 EVT_MENU(Menu_Toggle, RegFrame::OnToggle)
262 EVT_MENU(Menu_Refresh, RegFrame::OnRefresh)
263 EVT_MENU(Menu_Delete, RegFrame::OnDelete)
264 EVT_MENU(Menu_NewKey, RegFrame::OnNewKey)
265 EVT_MENU(Menu_NewText, RegFrame::OnNewText)
266 EVT_MENU(Menu_NewBinary, RegFrame::OnNewBinary)
267 EVT_MENU(Menu_Info, RegFrame::OnInfo)
268 EVT_MENU(Menu_ViewDefault, RegFrame::OnViewChange)
269 EVT_MENU(Menu_View32, RegFrame::OnViewChange)
270 EVT_MENU(Menu_View64, RegFrame::OnViewChange)
271 END_EVENT_TABLE()
272
273 #if DO_REGTEST
274
275 BEGIN_EVENT_TABLE(RegTreeCtrl, wxTreeCtrl)
276 EVT_TREE_DELETE_ITEM (Ctrl_RegTree, RegTreeCtrl::OnDeleteItem)
277 EVT_TREE_ITEM_EXPANDING (Ctrl_RegTree, RegTreeCtrl::OnItemExpanding)
278 EVT_TREE_ITEM_COLLAPSING(Ctrl_RegTree, RegTreeCtrl::OnItemExpanding)
279 EVT_TREE_SEL_CHANGED (Ctrl_RegTree, RegTreeCtrl::OnSelChanged)
280
281 EVT_TREE_BEGIN_LABEL_EDIT(Ctrl_RegTree, RegTreeCtrl::OnBeginEdit)
282 EVT_TREE_END_LABEL_EDIT (Ctrl_RegTree, RegTreeCtrl::OnEndEdit)
283
284 EVT_TREE_BEGIN_DRAG (Ctrl_RegTree, RegTreeCtrl::OnBeginDrag)
285 EVT_TREE_BEGIN_RDRAG (Ctrl_RegTree, RegTreeCtrl::OnBeginDrag)
286 EVT_TREE_END_DRAG (Ctrl_RegTree, RegTreeCtrl::OnEndDrag)
287
288 EVT_CHAR (RegTreeCtrl::OnChar)
289 EVT_RIGHT_DOWN(RegTreeCtrl::OnRightClick)
290 EVT_IDLE (RegTreeCtrl::OnIdle)
291 END_EVENT_TABLE()
292
293 #endif
294
295 // ============================================================================
296 // implementation
297 // ============================================================================
298
299 // ----------------------------------------------------------------------------
300 // global functions
301 // ----------------------------------------------------------------------------
302
303 // create the "registry operations" menu
304 wxMenu *CreateRegistryMenu()
305 {
306 wxMenu *pMenuNew = new wxMenu;
307 pMenuNew->Append(Menu_NewKey, wxT("&Key"), wxT("Create a new key"));
308 pMenuNew->AppendSeparator();
309 pMenuNew->Append(Menu_NewText, wxT("&Text value"), wxT("Create a new text value"));
310 pMenuNew->Append(Menu_NewBinary, wxT("&Binary value"), wxT("Create a new binary value"));
311
312 wxMenu *pMenuView = new wxMenu;
313 pMenuView->AppendRadioItem(
314 Menu_ViewDefault,
315 wxT("&Default"),
316 wxT("Default registry view for the program environment."));
317 pMenuView->AppendRadioItem(
318 Menu_View32,
319 wxT("32-bit Registry"),
320 wxT("View 32-bit registry."));
321 pMenuView->AppendRadioItem(
322 Menu_View64,
323 wxT("64-bit Registry"),
324 wxT("View 64-bit registry."));
325
326 wxMenu *pMenuReg = new wxMenu;
327 pMenuReg->Append(Menu_New, wxT("&New"), pMenuNew);
328 pMenuReg->Append(Menu_Delete, wxT("&Delete..."), wxT("Delete selected key/value"));
329 pMenuReg->AppendSeparator();
330 pMenuReg->Append(Menu_GoTo, wxT("&Go to...\tCtrl-G"), wxT("Go to registry key"));
331 pMenuReg->Append(Menu_Expand, wxT("&Expand"), wxT("Expand current key"));
332 pMenuReg->Append(Menu_Collapse, wxT("&Collapse"), wxT("Collapse current key"));
333 pMenuReg->Append(Menu_Toggle, wxT("&Toggle"), wxT("Toggle current key"));
334 pMenuReg->AppendSeparator();
335 pMenuReg->Append(Menu_Refresh, wxT("&Refresh"), wxT("Refresh the subtree"));
336 pMenuReg->Append(Menu_View, wxT("&View"), pMenuView);
337 pMenuReg->AppendSeparator();
338 pMenuReg->Append(Menu_Info, wxT("&Properties"),wxT("Information about current selection"));
339
340 return pMenuReg;
341 }
342
343 // ----------------------------------------------------------------------------
344 // application class
345 // ----------------------------------------------------------------------------
346 IMPLEMENT_APP(RegApp)
347
348 // `Main program' equivalent, creating windows and returning main app frame
349 bool RegApp::OnInit()
350 {
351 if ( !wxApp::OnInit() )
352 return false;
353
354 // create the main frame window and show it
355 RegFrame *frame = new RegFrame(NULL, wxT("wxRegTest"), 50, 50, 600, 350);
356 frame->Show(true);
357
358 return true;
359 }
360
361 // ----------------------------------------------------------------------------
362 // RegFrame
363 // ----------------------------------------------------------------------------
364
365 RegFrame::RegFrame(wxFrame *parent, const wxChar *title, int x, int y, int w, int h)
366 : wxFrame(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
367 {
368 // this reduces flicker effects
369 SetBackgroundColour(*wxWHITE);
370
371 // set the icon
372 // ------------
373 SetIcon(wxIcon(wxT("app_icon")));
374
375 // create menu
376 // -----------
377 wxMenu *pMenuFile = new wxMenu;
378 pMenuFile->Append(Menu_Test, wxT("Te&st"), wxT("Test key creation"));
379 pMenuFile->AppendSeparator();
380 pMenuFile->Append(Menu_About, wxT("&About"), wxT("Show an extraordinarly beautiful dialog"));
381 pMenuFile->AppendSeparator();
382 pMenuFile->Append(Menu_Quit, wxT("E&xit"), wxT("Quit this program"));
383
384 wxMenuBar *pMenu = new wxMenuBar;
385 pMenu->Append(pMenuFile, wxT("&File"));
386 pMenu->Append(CreateRegistryMenu(), wxT("&Registry"));
387 SetMenuBar(pMenu);
388
389 #if DO_REGTEST
390 // create child controls
391 // ---------------------
392 m_treeCtrl = new RegTreeCtrl(this, Ctrl_RegTree);
393 #endif
394
395 #if wxUSE_STATUSBAR
396 // create the status line
397 // ----------------------
398 CreateStatusBar(2);
399 #endif // wxUSE_STATUSBAR
400 }
401
402 RegFrame::~RegFrame()
403 {
404 #if DO_REGTEST
405 // this makes deletion of it *much* quicker
406 m_treeCtrl->Hide();
407 #endif
408 }
409
410 void RegFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
411 {
412 Close(true);
413 }
414
415 void RegFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
416 {
417 wxMessageDialog dialog(this,
418 wxT("wxRegistry sample\n")
419 wxT("(c) 1998, 2000 Vadim Zeitlin"),
420 wxT("About wxRegTest"), wxOK);
421
422 dialog.ShowModal();
423 }
424
425 void RegFrame::OnTest(wxCommandEvent& WXUNUSED(event))
426 {
427 #if DO_REGTEST
428 m_treeCtrl->OnMenuTest();
429 #endif
430 }
431
432 void RegFrame::OnGoTo(wxCommandEvent& WXUNUSED(event))
433 {
434 static wxString s_location = wxT("HKEY_CURRENT_USER\\Software\\wxWidgets");
435
436 wxString location = wxGetTextFromUser(
437 wxT("Enter the location to go to:"),
438 wxT("wxRegTest question"),
439 s_location,
440 this);
441
442 if ( !location )
443 return;
444
445 s_location = location;
446 #if DO_REGTEST
447 m_treeCtrl->GoTo(location);
448 #endif
449 }
450
451 void RegFrame::OnExpand(wxCommandEvent& WXUNUSED(event))
452 {
453 #if DO_REGTEST
454 m_treeCtrl->Expand(m_treeCtrl->GetSelection());
455 #endif
456 }
457
458 void RegFrame::OnCollapse(wxCommandEvent& WXUNUSED(event))
459 {
460 #if DO_REGTEST
461 m_treeCtrl->Collapse(m_treeCtrl->GetSelection());
462 #endif
463 }
464
465 void RegFrame::OnToggle(wxCommandEvent& WXUNUSED(event))
466 {
467 #if DO_REGTEST
468 m_treeCtrl->Toggle(m_treeCtrl->GetSelection());
469 #endif
470 }
471
472 void RegFrame::OnRefresh(wxCommandEvent& WXUNUSED(event))
473 {
474 #if DO_REGTEST
475 m_treeCtrl->DoRefresh();
476 #endif
477 }
478
479 void RegFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
480 {
481 #if DO_REGTEST
482 m_treeCtrl->DeleteSelected();
483 #endif
484 }
485
486 void RegFrame::OnNewKey(wxCommandEvent& WXUNUSED(event))
487 {
488 #if DO_REGTEST
489 if ( m_treeCtrl->IsKeySelected() )
490 {
491 m_treeCtrl->CreateNewKey(
492 wxGetTextFromUser(wxT("Enter the name of the new key")));
493 }
494 #endif
495 }
496
497 void RegFrame::OnNewText(wxCommandEvent& WXUNUSED(event))
498 {
499 #if DO_REGTEST
500 if ( m_treeCtrl->IsKeySelected() )
501 {
502 m_treeCtrl->CreateNewTextValue(
503 wxGetTextFromUser(wxT("Enter the name for the new text value")));
504 }
505 #endif
506 }
507
508 void RegFrame::OnNewBinary(wxCommandEvent& WXUNUSED(event))
509 {
510 #if DO_REGTEST
511 if ( m_treeCtrl->IsKeySelected() )
512 {
513 m_treeCtrl->CreateNewBinaryValue(
514 wxGetTextFromUser(wxT("Enter the name for the new binary value")));
515 }
516 #endif
517 }
518
519 void RegFrame::OnInfo(wxCommandEvent& WXUNUSED(event))
520 {
521 #if DO_REGTEST
522 m_treeCtrl->ShowProperties();
523 #endif
524 }
525
526 void RegFrame::OnViewChange(wxCommandEvent& event)
527 {
528 #if DO_REGTEST
529 wxRegKey::WOW64ViewMode view;
530 switch ( event.GetId() )
531 {
532 case Menu_ViewDefault:
533 view = wxRegKey::WOW64ViewMode_Default;
534 break;
535
536 case Menu_View32:
537 view = wxRegKey::WOW64ViewMode_32;
538 break;
539
540 case Menu_View64:
541 view = wxRegKey::WOW64ViewMode_64;
542 break;
543
544 default:
545 wxFAIL_MSG("Unexpected event source for view change.");
546 return;
547 }
548
549 m_treeCtrl->SetRegistryView(view);
550 #endif
551 }
552
553 // ----------------------------------------------------------------------------
554 // RegImageList
555 // ----------------------------------------------------------------------------
556 RegImageList::RegImageList() : wxImageList(16, 16, true)
557 {
558 // should be in sync with enum RegImageList::RegIcon
559 static const wxChar *aszIcons[] = { wxT("key1"),wxT("key2"),wxT("key3"),wxT("value1"),wxT("value2") };
560 wxString str = wxT("icon_");
561 for ( unsigned int n = 0; n < WXSIZEOF(aszIcons); n++ )
562 {
563 Add(wxIcon(str + aszIcons[n], wxBITMAP_TYPE_ICO_RESOURCE));
564 }
565 }
566
567 #if DO_REGTEST
568
569 // ----------------------------------------------------------------------------
570 // RegTreeCtrl
571 // ----------------------------------------------------------------------------
572
573 // create a new tree item and insert it into the tree
574 RegTreeCtrl::TreeNode *RegTreeCtrl::InsertNewTreeNode(
575 TreeNode *pParent,
576 const wxString& strName,
577 int idImage,
578 const wxString *pstrValue,
579 wxRegKey::WOW64ViewMode viewMode)
580 {
581 // create new item & insert it
582 TreeNode *pNewNode = new TreeNode;
583 pNewNode->m_pTree = this;
584 pNewNode->m_pParent = pParent;
585 pNewNode->m_strName = strName;
586 pNewNode->m_bKey = pstrValue == NULL;
587 pNewNode->m_pKey = NULL;
588 pNewNode->m_viewMode = viewMode;
589 if (pParent)
590 {
591 pNewNode->m_id = AppendItem(pParent->Id(),
592 pNewNode->IsKey() ? strName : *pstrValue,
593 idImage);
594 }
595 else
596 {
597 pNewNode->m_id = AddRoot(strName);
598 }
599
600 wxASSERT_MSG( pNewNode->m_id, wxT("can't create tree control item!"));
601
602 // save the pointer in the item
603 SetItemData(pNewNode->m_id, pNewNode);
604
605 // add it to the list of parent's children
606 if ( pParent != NULL )
607 {
608 pParent->m_aChildren.Add(pNewNode);
609 }
610
611 if ( pNewNode->IsKey() )
612 {
613 SetItemHasChildren(pNewNode->Id());
614
615 if ( !pNewNode->IsRoot() )
616 {
617 // set the expanded icon as well
618 SetItemImage(pNewNode->Id(),
619 RegImageList::OpenedKey,
620 wxTreeItemIcon_Expanded);
621 }
622 }
623
624 return pNewNode;
625 }
626
627 RegTreeCtrl::RegTreeCtrl(wxWindow *parent, wxWindowID id)
628 : wxTreeCtrl(parent, id, wxDefaultPosition, wxDefaultSize,
629 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS | wxSUNKEN_BORDER)
630 {
631 // init members
632 m_draggedItem = NULL;
633 m_restoreStatus = false;
634 m_viewMode = wxRegKey::WOW64ViewMode_Default;
635
636 // create the image list
637 // ---------------------
638 m_imageList = new RegImageList;
639 SetImageList(m_imageList);
640
641 // create root keys
642 // ----------------
643 m_pRoot =
644 InsertNewTreeNode(
645 NULL,
646 wxT("Registry Root"),
647 RegImageList::Root,
648 NULL,
649 m_viewMode);
650
651 // create popup menu
652 // -----------------
653 m_pMenuPopup = CreateRegistryMenu();
654 }
655
656 RegTreeCtrl::~RegTreeCtrl()
657 {
658 delete m_pMenuPopup;
659 // delete m_pRoot; -- this is done by the tree now
660 delete m_imageList;
661 }
662
663 void RegTreeCtrl::AddStdKeys()
664 {
665 for ( unsigned int ui = 0; ui < wxRegKey::nStdKeys; ui++ )
666 {
667 InsertNewTreeNode(
668 m_pRoot,
669 wxRegKey::GetStdKeyName(ui),
670 RegImageList::ClosedKey,
671 NULL,
672 m_viewMode);
673 }
674 }
675
676 // ----------------------------------------------------------------------------
677 // notifications
678 // ----------------------------------------------------------------------------
679
680 void RegTreeCtrl::OnIdle(wxIdleEvent& WXUNUSED(event))
681 {
682 if ( m_restoreStatus )
683 {
684 // restore it after OnItemExpanding()
685 wxLogStatus(wxT("Ok"));
686 wxSetCursor(*wxSTANDARD_CURSOR);
687
688 m_restoreStatus = false;
689 }
690 }
691
692 void RegTreeCtrl::OnRightClick(wxMouseEvent& event)
693 {
694 int iFlags;
695 wxTreeItemId lId = HitTest(wxPoint(event.GetX(), event.GetY()), iFlags);
696 if ( iFlags & wxTREE_HITTEST_ONITEMLABEL )
697 {
698 // select the item first
699 SelectItem(lId);
700 }
701 //else: take the currently selected item if click not on item
702
703 PopupMenu(m_pMenuPopup, event.GetX(), event.GetY());
704 }
705
706
707 void RegTreeCtrl::OnDeleteItem(wxTreeEvent& WXUNUSED(event))
708 {
709 }
710
711 // test the key creation functions
712 void RegTreeCtrl::OnMenuTest()
713 {
714 wxTreeItemId lId = GetSelection();
715 TreeNode *pNode = (TreeNode *)GetItemData(lId);
716
717 wxCHECK_RET( pNode != NULL, wxT("tree item without data?") );
718
719 if ( pNode->IsRoot() )
720 {
721 wxLogError(wxT("Can't create a subkey under the root key."));
722 return;
723 }
724
725 if ( !pNode->IsKey() )
726 {
727 wxLogError(wxT("Can't create a subkey under a value!"));
728 return;
729 }
730
731 wxRegKey key1(pNode->Key(), wxT("key1"));
732 if ( key1.Create() )
733 {
734 wxRegKey key2a(key1, wxT("key2a")), key2b(key1, wxT("key2b"));
735 if ( key2a.Create() && key2b.Create() )
736 {
737 // put some values under the newly created keys
738 key1.SetValue(wxT("first_term"), wxT("10"));
739 key1.SetValue(wxT("second_term"), wxT("7"));
740 key2a = wxT("this is the unnamed value");
741 key2b.SetValue(wxT("sum"), 17);
742
743 // refresh tree
744 pNode->Refresh();
745 wxLogStatus(wxT("Test keys successfully added."));
746 return;
747 }
748 }
749
750 wxLogError(wxT("Creation of test keys failed."));
751 }
752
753 void RegTreeCtrl::OnChar(wxKeyEvent& event)
754 {
755 switch ( event.GetKeyCode() )
756 {
757 case WXK_DELETE:
758 DeleteSelected();
759 return;
760
761 case WXK_RETURN:
762 if ( event.AltDown() )
763 {
764 ShowProperties();
765
766 return;
767 }
768 }
769
770 event.Skip();
771 }
772
773 void RegTreeCtrl::OnSelChanged(wxTreeEvent& event)
774 {
775 #if wxUSE_STATUSBAR
776 wxFrame *pFrame = (wxFrame *) wxWindow::GetParent();
777 pFrame->SetStatusText(GetNode(event)->FullName(), 1);
778 #else
779 wxUnusedVar(event);
780 #endif // wxUSE_STATUSBAR
781 }
782
783 void RegTreeCtrl::OnItemExpanding(wxTreeEvent& event)
784 {
785 TreeNode *pNode = GetNode(event);
786 bool bExpanding = event.GetEventType() == wxEVT_TREE_ITEM_EXPANDING;
787
788 // expansion might take some time
789 wxSetCursor(*wxHOURGLASS_CURSOR);
790 wxLogStatus(wxT("Working..."));
791 wxYield(); // to give the status line a chance to refresh itself
792 m_restoreStatus = true; // some time later...
793
794 if ( pNode->IsKey() )
795 {
796 if ( bExpanding )
797 {
798 // expanding: add subkeys/values
799 if ( !pNode->OnExpand() )
800 return;
801 }
802 else
803 {
804 // collapsing: clean up
805 pNode->OnCollapse();
806 }
807 }
808 }
809
810 void RegTreeCtrl::OnBeginEdit(wxTreeEvent& event)
811 {
812 TreeNode *pNode = GetNode(event);
813 if ( pNode->IsRoot() || pNode->Parent()->IsRoot() )
814 {
815 wxLogStatus(wxT("This registry key can't be renamed."));
816
817 event.Veto();
818 }
819 else
820 {
821 m_nameOld = pNode->m_strName;
822 }
823 }
824
825 void RegTreeCtrl::OnEndEdit(wxTreeEvent& event)
826 {
827 bool ok;
828
829 wxString name = event.GetLabel();
830
831 TreeNode *pNode = GetNode(event);
832 if ( pNode->IsKey() )
833 {
834 wxRegKey& key = pNode->Key();
835 ok = key.Rename(name);
836 }
837 else
838 {
839 pNode = pNode->Parent();
840 wxRegKey& key = pNode->Key();
841
842 ok = key.RenameValue(m_nameOld, name);
843 }
844
845 if ( !ok )
846 {
847 wxLogError(wxT("Failed to rename '%s' to '%s'."),
848 m_nameOld.c_str(), name.c_str());
849 }
850 #if 0 // MSW tree ctrl doesn't like this at all, it hangs
851 else
852 {
853 pNode->Refresh();
854 }
855 #endif // 0
856 }
857
858 void RegTreeCtrl::OnBeginDrag(wxTreeEvent& event)
859 {
860 m_copyOnDrop = event.GetEventType() == wxEVT_TREE_BEGIN_DRAG;
861
862 TreeNode *pNode = GetNode(event);
863 if ( pNode->IsRoot() || pNode->Parent()->IsRoot() )
864 {
865 wxLogStatus(wxT("This registry key can't be %s."),
866 m_copyOnDrop ? wxT("copied") : wxT("moved"));
867 }
868 else
869 {
870 wxLogStatus(wxT("%s item %s..."),
871 m_copyOnDrop ? wxT("Copying") : wxT("Moving"),
872 pNode->FullName());
873
874 m_draggedItem = pNode;
875
876 event.Allow();
877 }
878 }
879
880 void RegTreeCtrl::OnEndDrag(wxTreeEvent& event)
881 {
882 wxCHECK_RET( m_draggedItem, wxT("end drag without begin drag?") );
883
884 // clear the pointer anyhow
885 TreeNode *src = m_draggedItem;
886 m_draggedItem = NULL;
887
888 // where are we going to drop it?
889 TreeNode *dst = GetNode(event);
890 if ( dst && !dst->IsKey() )
891 {
892 // we need a parent key
893 dst = dst->Parent();
894 }
895
896 if ( !dst || dst->IsRoot() )
897 {
898 wxLogError(wxT("Can't create a key here."));
899
900 return;
901 }
902
903 bool isKey = src->IsKey();
904 if ( (isKey && (src == dst)) ||
905 (!isKey && (dst->Parent() == src)) ) {
906 wxLogStatus(wxT("Can't copy something on itself"));
907
908 return;
909 }
910
911 // remove the "Registry Root\\" from the full name
912 wxString nameSrc, nameDst;
913 nameSrc << wxString(src->FullName()).AfterFirst('\\');
914 nameDst << wxString(dst->FullName()).AfterFirst('\\') << '\\'
915 << wxString(src->FullName()).AfterLast('\\');
916
917 wxString verb = m_copyOnDrop ? wxT("copy") : wxT("move");
918 wxString what = isKey ? wxT("key") : wxT("value");
919
920 if ( wxMessageBox(wxString::Format
921 (
922 wxT("Do you really want to %s the %s %s to %s?"),
923 verb.c_str(),
924 what.c_str(),
925 nameSrc.c_str(),
926 nameDst.c_str()
927 ),
928 wxT("RegTest Confirm"),
929 wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) {
930 return;
931 }
932
933 bool ok;
934 if ( isKey )
935 {
936 wxRegKey& key = src->Key();
937 wxRegKey keyDst(dst->Key(), src->m_strName);
938 ok = keyDst.Create(false);
939 if ( !ok )
940 {
941 wxLogError(wxT("Key '%s' already exists"), keyDst.GetName().c_str());
942 }
943 else
944 {
945 ok = key.Copy(keyDst);
946 }
947
948 if ( ok && !m_copyOnDrop )
949 {
950 // delete the old key
951 ok = key.DeleteSelf();
952 if ( ok )
953 {
954 src->Parent()->Refresh();
955 }
956 }
957 }
958 else // value
959 {
960 wxRegKey& key = src->Parent()->Key();
961 ok = key.CopyValue(src->m_strName, dst->Key());
962 if ( ok && !m_copyOnDrop )
963 {
964 // we moved it, so delete the old one
965 ok = key.DeleteValue(src->m_strName);
966 }
967 }
968
969 if ( !ok )
970 {
971 wxLogError(wxT("Failed to %s registry %s."),
972 verb.c_str(), what.c_str());
973 }
974 else
975 {
976 dst->Refresh();
977 }
978 }
979
980 // ----------------------------------------------------------------------------
981 // TreeNode implementation
982 // ----------------------------------------------------------------------------
983 bool RegTreeCtrl::TreeNode::OnExpand()
984 {
985 // we add children only once
986 if ( !m_aChildren.IsEmpty() )
987 {
988 // we've been already expanded
989 return true;
990 }
991
992 if ( IsRoot() )
993 {
994 // we're the root key
995 m_pTree->AddStdKeys();
996 return true;
997 }
998
999 if ( Parent()->IsRoot() )
1000 {
1001 // we're a standard key
1002 m_pKey = new wxRegKey(m_strName, m_viewMode);
1003 }
1004 else
1005 {
1006 // we're a normal key
1007 m_pKey = new wxRegKey(*(Parent()->m_pKey), m_strName);
1008 }
1009
1010 if ( !m_pKey->Open() )
1011 {
1012 wxLogError(wxT("The key '%s' can't be opened."), FullName());
1013 return false;
1014 }
1015
1016 // if we're empty, we shouldn't be expandable at all
1017 bool isEmpty = true;
1018
1019 // enumeration variables
1020 long l;
1021 wxString str;
1022 bool bCont;
1023
1024 // enumerate all subkeys
1025 bCont = m_pKey->GetFirstKey(str, l);
1026 while ( bCont )
1027 {
1028 m_pTree->InsertNewTreeNode(
1029 this,
1030 str,
1031 RegImageList::ClosedKey,
1032 NULL,
1033 m_viewMode);
1034 bCont = m_pKey->GetNextKey(str, l);
1035
1036 // we have at least this key...
1037 isEmpty = false;
1038 }
1039
1040 // enumerate all values
1041 bCont = m_pKey->GetFirstValue(str, l);
1042 while ( bCont )
1043 {
1044 wxString strItem;
1045 if (str.empty())
1046 strItem = wxT("<default>");
1047 else
1048 strItem = str;
1049 strItem += wxT(" = ");
1050
1051 // determine the appropriate icon
1052 RegImageList::Icon icon;
1053 switch ( m_pKey->GetValueType(str) )
1054 {
1055 case wxRegKey::Type_String:
1056 case wxRegKey::Type_Expand_String:
1057 case wxRegKey::Type_Multi_String:
1058 {
1059 wxString strValue;
1060 icon = RegImageList::TextValue;
1061 m_pKey->QueryValue(str, strValue);
1062 strItem += strValue;
1063 }
1064 break;
1065
1066 case wxRegKey::Type_None:
1067 // @@ handle the error...
1068 icon = RegImageList::BinaryValue;
1069 break;
1070
1071 case wxRegKey::Type_Dword:
1072 {
1073 long l;
1074 m_pKey->QueryValue(str, &l);
1075 strItem << l;
1076 }
1077
1078 // fall through
1079
1080 default:
1081 icon = RegImageList::BinaryValue;
1082 }
1083
1084 m_pTree->InsertNewTreeNode(this, str, icon, &strItem, m_viewMode);
1085 bCont = m_pKey->GetNextValue(str, l);
1086
1087 // we have at least this value...
1088 isEmpty = false;
1089 }
1090
1091 if ( isEmpty )
1092 {
1093 // this is for the case when our last child was just deleted
1094 wxTreeItemId theId(Id()); // Temp variable seems necessary for BC++
1095 m_pTree->Collapse(theId);
1096
1097 // we won't be expanded any more
1098 m_pTree->SetItemHasChildren(theId, false);
1099 }
1100
1101 return true;
1102 }
1103
1104 void RegTreeCtrl::TreeNode::OnCollapse()
1105 {
1106 DestroyChildren();
1107
1108 wxDELETE(m_pKey);
1109 }
1110
1111 void RegTreeCtrl::TreeNode::Refresh()
1112 {
1113 if ( !IsKey() )
1114 return;
1115
1116 wxTreeItemId theId(Id()); // Temp variable seems necessary for BC++
1117 bool wasExpanded = m_pTree->IsExpanded(theId);
1118 if ( wasExpanded )
1119 m_pTree->Collapse(theId);
1120
1121 OnCollapse();
1122 m_pTree->SetItemHasChildren(theId);
1123 if ( wasExpanded )
1124 {
1125 m_pTree->Expand(theId);
1126 OnExpand();
1127 }
1128 }
1129
1130 bool RegTreeCtrl::TreeNode::DeleteChild(TreeNode *child)
1131 {
1132 int index = m_aChildren.Index(child);
1133 wxCHECK_MSG( index != wxNOT_FOUND, false,
1134 wxT("our child in tree should be in m_aChildren") );
1135
1136 m_aChildren.RemoveAt((size_t)index);
1137
1138 bool ok;
1139 if ( child->IsKey() )
1140 {
1141 // must close key before deleting it
1142 child->OnCollapse();
1143
1144 ok = Key().DeleteKey(child->m_strName);
1145 }
1146 else
1147 {
1148 ok = Key().DeleteValue(child->m_strName);
1149 }
1150
1151 if ( ok )
1152 {
1153 wxTreeItemId theId(child->Id()); // Temp variable seems necessary for BC++
1154 m_pTree->Delete(theId);
1155
1156 Refresh();
1157 }
1158
1159 return ok;
1160 }
1161
1162 void RegTreeCtrl::TreeNode::DestroyChildren()
1163 {
1164 // destroy all children
1165 size_t nCount = m_aChildren.GetCount();
1166 for ( size_t n = 0; n < nCount; n++ )
1167 {
1168 wxTreeItemId lId = m_aChildren[n]->Id();
1169 m_pTree->Delete(lId);
1170 }
1171
1172 m_aChildren.Empty();
1173 }
1174
1175 RegTreeCtrl::TreeNode::~TreeNode()
1176 {
1177 delete m_pKey;
1178 }
1179
1180 const wxChar *RegTreeCtrl::TreeNode::FullName() const
1181 {
1182 static wxString s_strName;
1183
1184 if ( IsRoot() )
1185 {
1186 return wxT("Registry Root");
1187 }
1188 else
1189 {
1190 // our own registry key might not (yet) exist or we might be a value,
1191 // so just use the parent's and concatenate
1192 s_strName = Parent()->FullName();
1193 s_strName << wxT('\\') << m_strName;
1194
1195 return s_strName.t_str();
1196 }
1197 }
1198
1199 void RegTreeCtrl::TreeNode::SetRegistryView(wxRegKey::WOW64ViewMode viewMode)
1200 {
1201 m_viewMode = viewMode;
1202
1203 // Update children with new view.
1204 size_t nCount = m_aChildren.GetCount();
1205 for (size_t n = 0; n < nCount; n++)
1206 m_aChildren[n]->SetRegistryView(viewMode);
1207 }
1208
1209 // ----------------------------------------------------------------------------
1210 // operations on RegTreeCtrl
1211 // ----------------------------------------------------------------------------
1212
1213 void RegTreeCtrl::GoTo(const wxString& location)
1214 {
1215 wxStringTokenizer tk(location, wxT("\\"));
1216
1217 wxTreeItemId id = GetRootItem();
1218
1219 while ( tk.HasMoreTokens() )
1220 {
1221 wxString subkey = tk.GetNextToken();
1222
1223 wxTreeItemId idCurrent = id;
1224 if ( !IsExpanded(idCurrent) )
1225 Expand(idCurrent);
1226
1227 wxTreeItemIdValue dummy;
1228 id = GetFirstChild(idCurrent, dummy);
1229
1230 if ( idCurrent == GetRootItem() )
1231 {
1232 // special case: we understand both HKCU and HKEY_CURRENT_USER here
1233 for ( size_t key = 0; key < wxRegKey::nStdKeys; key++ )
1234 {
1235 if ( subkey == wxRegKey::GetStdKeyName(key)
1236 || subkey == wxRegKey::GetStdKeyShortName(key) )
1237 {
1238 break;
1239 }
1240
1241 id = GetNextChild(idCurrent, dummy);
1242 }
1243 }
1244 else
1245 {
1246 // enum all children
1247 while ( id.IsOk() )
1248 {
1249 if ( subkey == ((TreeNode *)GetItemData(id))->m_strName )
1250 break;
1251
1252 id = GetNextChild(idCurrent, dummy);
1253 }
1254 }
1255
1256 if ( !id.IsOk() )
1257 {
1258 wxLogError(wxT("No such key '%s'."), location.c_str());
1259
1260 return;
1261 }
1262 }
1263
1264 if ( id.IsOk() )
1265 SelectItem(id);
1266 }
1267
1268 void RegTreeCtrl::DeleteSelected()
1269 {
1270 wxTreeItemId lCurrent = GetSelection(),
1271 lParent = GetItemParent(lCurrent);
1272
1273 if ( lParent == GetRootItem() )
1274 {
1275 wxLogError(wxT("Can't delete root key."));
1276 return;
1277 }
1278
1279 TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent),
1280 *pParent = (TreeNode *)GetItemData(lParent);
1281
1282 wxCHECK_RET(pCurrent && pParent, wxT("either node or parent without data?"));
1283
1284 if ( pParent->IsRoot() )
1285 {
1286 wxLogError(wxT("Can't delete standard key."));
1287 return;
1288 }
1289
1290 wxString what = pCurrent->IsKey() ? wxT("key") : wxT("value");
1291 if ( wxMessageBox(wxString::Format
1292 (
1293 wxT("Do you really want to delete this %s?"),
1294 what.c_str()
1295 ),
1296 wxT("Confirmation"),
1297 wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES )
1298 {
1299 return;
1300 }
1301
1302 pParent->DeleteChild(pCurrent);
1303 }
1304
1305 void RegTreeCtrl::CreateNewKey(const wxString& strName)
1306 {
1307 wxTreeItemId lCurrent = GetSelection();
1308 TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent);
1309
1310 wxCHECK_RET( pCurrent != NULL, wxT("node without data?") );
1311
1312 wxASSERT( pCurrent->IsKey() ); // check must have been done before
1313
1314 if ( pCurrent->IsRoot() )
1315 {
1316 wxLogError(wxT("Can't create a new key under the root key."));
1317 return;
1318 }
1319
1320 wxRegKey key(pCurrent->Key(), strName);
1321 if ( key.Create() )
1322 pCurrent->Refresh();
1323 }
1324
1325 void RegTreeCtrl::CreateNewTextValue(const wxString& strName)
1326 {
1327 wxTreeItemId lCurrent = GetSelection();
1328 TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent);
1329
1330 wxCHECK_RET( pCurrent != NULL, wxT("node without data?") );
1331
1332 wxASSERT( pCurrent->IsKey() ); // check must have been done before
1333
1334 if ( pCurrent->IsRoot() )
1335 {
1336 wxLogError(wxT("Can't create a new value under the root key."));
1337 return;
1338 }
1339
1340 if ( pCurrent->Key().SetValue(strName, wxEmptyString) )
1341 pCurrent->Refresh();
1342 }
1343
1344 void RegTreeCtrl::CreateNewBinaryValue(const wxString& strName)
1345 {
1346 wxTreeItemId lCurrent = GetSelection();
1347 TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent);
1348
1349 wxCHECK_RET( pCurrent != NULL, wxT("node without data?") );
1350
1351 wxASSERT( pCurrent->IsKey() ); // check must have been done before
1352
1353 if ( pCurrent->IsRoot() )
1354 {
1355 wxLogError(wxT("Can't create a new value under the root key."));
1356 return;
1357 }
1358
1359 if ( pCurrent->Key().SetValue(strName, 0) )
1360 pCurrent->Refresh();
1361 }
1362
1363 void RegTreeCtrl::SetRegistryView(wxRegKey::WOW64ViewMode viewMode)
1364 {
1365 m_viewMode = viewMode;
1366 m_pRoot->SetRegistryView(viewMode);
1367 m_pRoot->Refresh();
1368 }
1369
1370 void RegTreeCtrl::ShowProperties()
1371 {
1372 wxTreeItemId lCurrent = GetSelection();
1373 TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent);
1374
1375 if ( !pCurrent || pCurrent->IsRoot() )
1376 {
1377 wxLogStatus(wxT("No properties"));
1378
1379 return;
1380 }
1381
1382 if ( pCurrent->IsKey() )
1383 {
1384 const wxRegKey& key = pCurrent->Key();
1385 size_t nSubKeys, nValues;
1386 if ( !key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
1387 {
1388 wxLogError(wxT("Couldn't get key info"));
1389 }
1390 else
1391 {
1392 wxLogMessage(wxT("Key '%s' has %u subkeys and %u values."),
1393 key.GetName().c_str(), nSubKeys, nValues);
1394 }
1395 }
1396 else // it's a value
1397 {
1398 TreeNode *parent = pCurrent->Parent();
1399 wxCHECK_RET( parent, wxT("reg value without key?") );
1400
1401 const wxRegKey& key = parent->Key();
1402 const wxChar *value = pCurrent->m_strName.c_str();
1403 wxLogMessage(wxT("Value '%s' under the key '%s' is of type ")
1404 wxT("%d (%s)."),
1405 value,
1406 parent->m_strName.c_str(),
1407 key.GetValueType(value),
1408 key.IsNumericValue(value) ? wxT("numeric") : wxT("string"));
1409
1410 }
1411 }
1412
1413 bool RegTreeCtrl::IsKeySelected() const
1414 {
1415 wxTreeItemId lCurrent = GetSelection();
1416 TreeNode *pCurrent = (TreeNode *) GetItemData(lCurrent);
1417
1418 wxCHECK( pCurrent != NULL, false );
1419
1420 return pCurrent->IsKey();
1421 }
1422
1423 void RegTreeCtrl::DoRefresh()
1424 {
1425 wxTreeItemId lId = GetSelection();
1426 if ( !lId )
1427 return;
1428
1429 TreeNode *pNode = (TreeNode *) GetItemData(lId);
1430
1431 wxCHECK_RET( pNode != NULL, wxT("tree item without data?") );
1432
1433 pNode->Refresh();
1434 }
1435
1436 #endif