// create a new tree item and insert it into the tree RegTreeCtrl::TreeNode *RegTreeCtrl::InsertNewTreeNode( TreeNode *pParent, const wxString& strName, int idImage, const wxString *pstrValue, wxRegKey::WOW64ViewMode viewMode) { // create new item & insert it TreeNode *pNewNode = new TreeNode; pNewNode->m_pTree = this; pNewNode->m_pParent = pParent; pNewNode->m_strName = strName; pNewNode->m_bKey = pstrValue == NULL; pNewNode->m_pKey = NULL; pNewNode->m_viewMode = viewMode; if (pParent) { pNewNode->m_id = AppendItem(pParent->Id(), pNewNode->IsKey() ? strName : *pstrValue, idImage); } else { pNewNode->m_id = AddRoot(strName); } wxASSERT_MSG( pNewNode->m_id, wxT("can't create tree control item!")); // save the pointer in the item SetItemData(pNewNode->m_id, pNewNode); // add it to the list of parent's children if ( pParent != NULL ) { pParent->m_aChildren.Add(pNewNode); } if ( pNewNode->IsKey() ) { SetItemHasChildren(pNewNode->Id()); if ( !pNewNode->IsRoot() ) { // set the expanded icon as well SetItemImage(pNewNode->Id(), RegImageList::OpenedKey, wxTreeItemIcon_Expanded); } } return pNewNode; }
void RegTreeCtrl::OnEndEdit(wxTreeEvent& event) { bool ok; wxString name = event.GetLabel(); TreeNode *pNode = GetNode(event); if ( pNode->IsKey() ) { wxRegKey& key = pNode->Key(); ok = key.Rename(name); } else { pNode = pNode->Parent(); wxRegKey& key = pNode->Key(); ok = key.RenameValue(m_nameOld, name); } if ( !ok ) { wxLogError(wxT("Failed to rename '%s' to '%s'."), m_nameOld.c_str(), name.c_str()); } #if 0 // MSW tree ctrl doesn't like this at all, it hangs else { pNode->Refresh(); } #endif // 0 }
void RegTreeCtrl::OnItemExpanding(wxTreeEvent& event) { TreeNode *pNode = GetNode(event); bool bExpanding = event.GetEventType() == wxEVT_TREE_ITEM_EXPANDING; // expansion might take some time wxSetCursor(*wxHOURGLASS_CURSOR); wxLogStatus(wxT("Working...")); wxYield(); // to give the status line a chance to refresh itself m_restoreStatus = true; // some time later... if ( pNode->IsKey() ) { if ( bExpanding ) { // expanding: add subkeys/values if ( !pNode->OnExpand() ) return; } else { // collapsing: clean up pNode->OnCollapse(); } } }
bool RegTreeCtrl::IsKeySelected() const { wxTreeItemId lCurrent = GetSelection(); TreeNode *pCurrent = (TreeNode *) GetItemData(lCurrent); wxCHECK( pCurrent != NULL, false ); return pCurrent->IsKey(); }
void RegTreeCtrl::CreateNewBinaryValue(const wxString& strName) { wxTreeItemId lCurrent = GetSelection(); TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); wxCHECK_RET( pCurrent != NULL, wxT("node without data?") ); wxASSERT( pCurrent->IsKey() ); // check must have been done before if ( pCurrent->IsRoot() ) { wxLogError(wxT("Can't create a new value under the root key.")); return; } if ( pCurrent->Key().SetValue(strName, 0) ) pCurrent->Refresh(); }
void RegTreeCtrl::ShowProperties() { wxTreeItemId lCurrent = GetSelection(); TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent); if ( !pCurrent || pCurrent->IsRoot() ) { wxLogStatus(wxT("No properties")); return; } if ( pCurrent->IsKey() ) { const wxRegKey& key = pCurrent->Key(); size_t nSubKeys, nValues; if ( !key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) ) { wxLogError(wxT("Couldn't get key info")); } else { wxLogMessage(wxT("Key '%s' has %u subkeys and %u values."), key.GetName().c_str(), nSubKeys, nValues); } } else // it's a value { TreeNode *parent = pCurrent->Parent(); wxCHECK_RET( parent, wxT("reg value without key?") ); const wxRegKey& key = parent->Key(); const wxChar *value = pCurrent->m_strName.c_str(); wxLogMessage(wxT("Value '%s' under the key '%s' is of type ") wxT("%d (%s)."), value, parent->m_strName.c_str(), key.GetValueType(value), key.IsNumericValue(value) ? wxT("numeric") : wxT("string")); } }
// test the key creation functions void RegTreeCtrl::OnMenuTest() { wxTreeItemId lId = GetSelection(); TreeNode *pNode = (TreeNode *)GetItemData(lId); wxCHECK_RET( pNode != NULL, wxT("tree item without data?") ); if ( pNode->IsRoot() ) { wxLogError(wxT("Can't create a subkey under the root key.")); return; } if ( !pNode->IsKey() ) { wxLogError(wxT("Can't create a subkey under a value!")); return; } wxRegKey key1(pNode->Key(), wxT("key1")); if ( key1.Create() ) { wxRegKey key2a(key1, wxT("key2a")), key2b(key1, wxT("key2b")); if ( key2a.Create() && key2b.Create() ) { // put some values under the newly created keys key1.SetValue(wxT("first_term"), wxT("10")); key1.SetValue(wxT("second_term"), wxT("7")); key2a = wxT("this is the unnamed value"); key2b.SetValue(wxT("sum"), 17); // refresh tree pNode->Refresh(); wxLogStatus(wxT("Test keys successfully added.")); return; } } wxLogError(wxT("Creation of test keys failed.")); }
void RegTreeCtrl::DeleteSelected() { wxTreeItemId lCurrent = GetSelection(), lParent = GetItemParent(lCurrent); if ( lParent == GetRootItem() ) { wxLogError(wxT("Can't delete root key.")); return; } TreeNode *pCurrent = (TreeNode *)GetItemData(lCurrent), *pParent = (TreeNode *)GetItemData(lParent); wxCHECK_RET(pCurrent && pParent, wxT("either node or parent without data?")); if ( pParent->IsRoot() ) { wxLogError(wxT("Can't delete standard key.")); return; } wxString what = pCurrent->IsKey() ? wxT("key") : wxT("value"); if ( wxMessageBox(wxString::Format ( wxT("Do you really want to delete this %s?"), what.c_str() ), wxT("Confirmation"), wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) { return; } pParent->DeleteChild(pCurrent); }
void RegTreeCtrl::OnEndDrag(wxTreeEvent& event) { wxCHECK_RET( m_draggedItem, wxT("end drag without begin drag?") ); // clear the pointer anyhow TreeNode *src = m_draggedItem; m_draggedItem = NULL; // where are we going to drop it? TreeNode *dst = GetNode(event); if ( dst && !dst->IsKey() ) { // we need a parent key dst = dst->Parent(); } if ( !dst || dst->IsRoot() ) { wxLogError(wxT("Can't create a key here.")); return; } bool isKey = src->IsKey(); if ( (isKey && (src == dst)) || (!isKey && (dst->Parent() == src)) ) { wxLogStatus(wxT("Can't copy something on itself")); return; } // remove the "Registry Root\\" from the full name wxString nameSrc, nameDst; nameSrc << wxString(src->FullName()).AfterFirst('\\'); nameDst << wxString(dst->FullName()).AfterFirst('\\') << '\\' << wxString(src->FullName()).AfterLast('\\'); wxString verb = m_copyOnDrop ? wxT("copy") : wxT("move"); wxString what = isKey ? wxT("key") : wxT("value"); if ( wxMessageBox(wxString::Format ( wxT("Do you really want to %s the %s %s to %s?"), verb.c_str(), what.c_str(), nameSrc.c_str(), nameDst.c_str() ), wxT("RegTest Confirm"), wxICON_QUESTION | wxYES_NO | wxCANCEL, this) != wxYES ) { return; } bool ok; if ( isKey ) { wxRegKey& key = src->Key(); wxRegKey keyDst(dst->Key(), src->m_strName); ok = keyDst.Create(false); if ( !ok ) { wxLogError(wxT("Key '%s' already exists"), keyDst.GetName().c_str()); } else { ok = key.Copy(keyDst); } if ( ok && !m_copyOnDrop ) { // delete the old key ok = key.DeleteSelf(); if ( ok ) { src->Parent()->Refresh(); } } } else // value { wxRegKey& key = src->Parent()->Key(); ok = key.CopyValue(src->m_strName, dst->Key()); if ( ok && !m_copyOnDrop ) { // we moved it, so delete the old one ok = key.DeleteValue(src->m_strName); } } if ( !ok ) { wxLogError(wxT("Failed to %s registry %s."), verb.c_str(), what.c_str()); } else { dst->Refresh(); } }