[REGEDIT] Implement "Add to favorites" feature (#6890)

The ability to go to a favorite and the dialog was already there, just needed to fill in the blanks.
This commit is contained in:
Whindmar Saksit 2024-05-21 09:31:24 +02:00 committed by GitHub
parent 108db8f007
commit 2198247bdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -39,6 +39,16 @@ extern WCHAR Suggestions[256];
* Local module support methods
*/
static UINT ErrorBox(HWND hWnd, UINT Error)
{
WCHAR buf[400];
if (!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, Error, 0, buf, _countof(buf), NULL))
*(UINT*)buf = L'?';
MessageBoxW(hWnd, buf, NULL, MB_ICONSTOP);
return Error;
}
static void resize_frame_rect(HWND hWnd, PRECT prect)
{
if (IsWindowVisible(hStatusBar))
@ -72,6 +82,8 @@ static void OnInitMenu(HWND hWnd)
static int s_nFavoriteMenuSubPos = -1;
HMENU hMenu;
BOOL bDisplayedAny = FALSE;
HTREEITEM hSelTreeItem;
BOOL bCanAddFav;
/* Find Favorites menu and clear it out */
hMenu = GetSubMenu(GetMenu(hWnd), FAVORITES_MENU_POSITION);
@ -86,6 +98,11 @@ static void OnInitMenu(HWND hWnd)
while(RemoveMenu(hMenu, s_nFavoriteMenuSubPos, MF_BYPOSITION)) ;
}
hSelTreeItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
bCanAddFav = TreeView_GetParent(g_pChildWnd->hTreeWnd, hSelTreeItem) != NULL;
EnableMenuItem(GetMenu(hWnd), ID_FAVOURITES_ADDTOFAVOURITES,
MF_BYCOMMAND | (bCanAddFav ? MF_ENABLED : MF_GRAYED));
lResult = RegOpenKeyW(HKEY_CURRENT_USER, s_szFavoritesRegKey, &hKey);
if (lResult != ERROR_SUCCESS)
goto done;
@ -807,6 +824,89 @@ done:
RegCloseKey(hKey);
}
static LPWSTR GetItemFullPath(HTREEITEM hTI)
{
HKEY hRoot;
WCHAR rootname[MAX_PATH], *buffer;
SIZE_T rootlen, subkeylen;
LPCWSTR subkey = GetItemPath(g_pChildWnd->hTreeWnd, hTI, &hRoot);
if (!subkey || !hRoot)
return NULL;
if (!GetKeyName(rootname, ARRAY_SIZE(rootname), hRoot, L""))
return NULL;
rootlen = lstrlenW(rootname) + 1; // + 1 for '\\'
subkeylen = lstrlenW(subkey);
buffer = (WCHAR*)malloc((rootlen + subkeylen + 1) * sizeof(WCHAR));
if (buffer)
{
lstrcpyW(buffer, rootname);
buffer[rootlen - 1] = '\\';
lstrcpyW(buffer + rootlen, subkey);
}
return buffer;
}
static INT_PTR CALLBACK AddToFavoritesDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND hName = GetDlgItem(hWnd, IDC_FAVORITENAME);
WCHAR name[MAX_PATH];
switch (uMsg)
{
case WM_INITDIALOG:
{
TVITEM tvi;
tvi.mask = TVIF_HANDLE | TVIF_TEXT;
tvi.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
tvi.pszText = name;
tvi.cchTextMax = _countof(name);
if (!TreeView_GetItem(g_pChildWnd->hTreeWnd, &tvi))
tvi.pszText[0] = UNICODE_NULL;
SetWindowTextW(hName, tvi.pszText);
SendMessageW(hName, EM_LIMITTEXT, _countof(name) - 1, 0);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
{
LPWSTR path;
HKEY hKey;
DWORD err;
if (!GetWindowTextW(hName, name, _countof(name)))
{
err = GetLastError();
goto failed;
}
path = GetItemFullPath(NULL);
if (!path)
{
err = ERROR_NOT_ENOUGH_MEMORY;
goto failed;
}
err = RegCreateKeyExW(HKEY_CURRENT_USER, s_szFavoritesRegKey, 0,
NULL, 0, KEY_SET_VALUE, NULL, &hKey, NULL);
if (err)
goto failed;
err = RegSetValueExW(hKey, name, 0, REG_SZ, (BYTE*)path, (lstrlenW(path) + 1) * sizeof(WCHAR));
RegCloseKey(hKey);
if (err) failed:
ErrorBox(hWnd, err);
free(path);
return EndDialog(hWnd, err);
}
case IDCANCEL:
return EndDialog(hWnd, ERROR_CANCELLED);
case IDC_FAVORITENAME:
if (HIWORD(wParam) == EN_UPDATE)
EnableWindow(GetDlgItem(hWnd, IDOK), GetWindowTextLengthW(hName) != 0);
break;
}
break;
}
return FALSE;
}
BOOL CopyKeyName(HWND hWnd, HKEY hRootKey, LPCWSTR keyName)
{
BOOL bClipboardOpened = FALSE;
@ -1119,6 +1219,9 @@ static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case ID_VIEW_STATUSBAR:
toggle_child(hWnd, LOWORD(wParam), hStatusBar);
return TRUE;
case ID_FAVOURITES_ADDTOFAVOURITES:
DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_ADDFAVORITES), hWnd, AddToFavoritesDlgProc);
return TRUE;
case ID_HELP_HELPTOPICS:
WinHelpW(hWnd, L"regedit", HELP_FINDER, 0);
return TRUE;