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