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