[REGEDIT] Fix and improvement in TreeView (Null handle check, incorrect display after edit, WM_NOTIFY refactor) (#2733)

* - Fix of missing invalid handle check
- Fix of Address & status bar incorrect display after rename
- Refactoring of Treeview WM_NOTIFY code, for better consistency with WM_NOTIFY management for ListView

* Fix following review (space in #define)

* Cleanup following review

* fix following review

* Fix of round 3 of review comments

* UpdateAdress kept in its original location

* fix

* fix (extern missing)

* both extern removed

* Review comments
Error message being added in order to support CORE-17048
This commit is contained in:
Kyle Katarn 2020-05-16 14:23:20 +02:00 committed by GitHub
parent 3fd6dbd943
commit 4d1cd72317
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 143 additions and 109 deletions

View file

@ -625,6 +625,110 @@ done:
return bSuccess;
}
BOOL TreeWndNotifyProc(HWND hWnd, WPARAM wParam, LPARAM lParam, BOOL *Result)
{
UNREFERENCED_PARAMETER(wParam);
*Result = TRUE;
switch (((LPNMHDR)lParam)->code)
{
case TVN_ITEMEXPANDING:
*Result = !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
return TRUE;
case TVN_SELCHANGED:
{
NMTREEVIEW* pnmtv = (NMTREEVIEW*)lParam;
/* Get the parent of the current item */
HTREEITEM hParentItem = TreeView_GetParent(g_pChildWnd->hTreeWnd, pnmtv->itemNew.hItem);
UpdateAddress(pnmtv->itemNew.hItem, NULL, NULL);
/* Disable the Permissions menu item for 'My Computer' */
EnableMenuItem(hMenuFrame, ID_EDIT_PERMISSIONS, MF_BYCOMMAND | (hParentItem ? MF_ENABLED : MF_GRAYED));
/*
* Disable Delete/Rename menu options for 'My Computer' (first item so doesn't have any parent)
* and HKEY_* keys (their parent is 'My Computer' and the previous remark applies).
*/
if (!hParentItem || !TreeView_GetParent(g_pChildWnd->hTreeWnd, hParentItem))
{
EnableMenuItem(hMenuFrame , ID_EDIT_DELETE, MF_BYCOMMAND | MF_GRAYED);
EnableMenuItem(hMenuFrame , ID_EDIT_RENAME, MF_BYCOMMAND | MF_GRAYED);
EnableMenuItem(hPopupMenus, ID_TREE_DELETE, MF_BYCOMMAND | MF_GRAYED);
EnableMenuItem(hPopupMenus, ID_TREE_RENAME, MF_BYCOMMAND | MF_GRAYED);
}
else
{
EnableMenuItem(hMenuFrame , ID_EDIT_DELETE, MF_BYCOMMAND | MF_ENABLED);
EnableMenuItem(hMenuFrame , ID_EDIT_RENAME, MF_BYCOMMAND | MF_ENABLED);
EnableMenuItem(hPopupMenus, ID_TREE_DELETE, MF_BYCOMMAND | MF_ENABLED);
EnableMenuItem(hPopupMenus, ID_TREE_RENAME, MF_BYCOMMAND | MF_ENABLED);
}
return TRUE;
}
case NM_SETFOCUS:
g_pChildWnd->nFocusPanel = 0;
break;
case TVN_BEGINLABELEDIT:
{
LPNMTVDISPINFO ptvdi = (LPNMTVDISPINFO) lParam;
/* cancel label edit for rootkeys */
if (!TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
!TreeView_GetParent(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem)))
{
*Result = TRUE;
}
else
{
*Result = FALSE;
}
return TRUE;
}
case TVN_ENDLABELEDIT:
{
LPCWSTR keyPath;
HKEY hRootKey;
HKEY hKey = NULL;
LPNMTVDISPINFO ptvdi = (LPNMTVDISPINFO) lParam;
LONG nRenResult;
LONG lResult = TRUE;
WCHAR szBuffer[MAX_PATH];
WCHAR Caption[128];
if (ptvdi->item.pszText)
{
keyPath = GetItemPath(g_pChildWnd->hTreeWnd, TreeView_GetParent(g_pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
if (wcslen(keyPath))
_snwprintf(szBuffer, COUNT_OF(szBuffer), L"%s\\%s", keyPath, ptvdi->item.pszText);
else
_snwprintf(szBuffer, COUNT_OF(szBuffer), L"%s", ptvdi->item.pszText);
keyPath = GetItemPath(g_pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
if (RegOpenKeyExW(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
lResult = FALSE;
RegCloseKey(hKey);
TreeView_EditLabel(g_pChildWnd->hTreeWnd, ptvdi->item.hItem);
}
else
{
nRenResult = RenameKey(hRootKey, keyPath, ptvdi->item.pszText);
if (nRenResult != ERROR_SUCCESS)
{
LoadStringW(hInst, IDS_ERROR, Caption, COUNT_OF(Caption));
ErrorMessageBox(hWnd, Caption, nRenResult);
lResult = FALSE;
}
else
UpdateAddress(ptvdi->item.hItem, hRootKey, szBuffer);
}
*Result = lResult;
}
return TRUE;
}
}
return FALSE;
}
/*
* CreateTreeView - creates a tree view control.
@ -642,6 +746,8 @@ HWND CreateTreeView(HWND hwndParent, LPWSTR pHostName, HMENU id)
WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | TVS_EDITLABELS | TVS_SHOWSELALWAYS,
0, 0, rcClient.right, rcClient.bottom,
hwndParent, id, hInst, NULL);
if (!hwndTV) return NULL;
/* Initialize the image list, and add items to the control. */
if (!InitTreeViewImageLists(hwndTV) || !InitTreeViewItems(hwndTV, pHostName))
{