Merge pull request #3863 from RaiKoHoff/Dev_Master

Fix "add to recent documents" issue
This commit is contained in:
Rainer Kottenhoff 2021-12-12 16:15:01 +01:00 committed by GitHub
commit 5899376b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 59 deletions

View File

@ -782,15 +782,17 @@ extern "C" bool IniFileIterateSection(const HPATHL hpthIniFile, LPCWSTR lpSectio
//
// AddFilePathToRecentDocs()
//
extern "C" void AddFilePathToRecentDocs(const HPATHL hpthIniFile)
extern "C" void AddFilePathToRecentDocs(const HPATHL hpthFile)
{
if (Path_IsEmpty(hpthIniFile)) {
if (Path_IsEmpty(hpthFile)) {
return;
}
HPATHL const hpth = Path_Copy(hpthFile);
Path_CanonicalizeEx(hpth, NULL);
if (Flags.ShellUseSystemMRU) {
#if TRUE
SHAddToRecentDocs(SHARD_PATHW, Path_Get(hpthIniFile));
SHAddToRecentDocs(SHARD_PATHW, Path_Get(hpth));
#else
(void)CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE);
@ -807,6 +809,7 @@ extern "C" void AddFilePathToRecentDocs(const HPATHL hpthIniFile)
CoUninitialize();
#endif
}
Path_Release(hpth);
}
@ -2189,7 +2192,7 @@ bool SaveAllSettings(bool bForceSaveSettings)
if (Globals.bCanSaveIniFile) {
if (!Settings.SaveRecentFiles) {
// Cleanup unwanted MRUs
MRU_Empty(Globals.pFileMRU, false);
MRU_Empty(Globals.pFileMRU, false, true);
MRU_Save(Globals.pFileMRU);
} else {
//int const cnt = MRU_Count(Globals.pFileMRU);
@ -2198,9 +2201,9 @@ bool SaveAllSettings(bool bForceSaveSettings)
if (!Settings.SaveFindReplace) {
// Cleanup unwanted MRUs
MRU_Empty(Globals.pMRUfind, false);
MRU_Empty(Globals.pMRUfind, false, true);
MRU_Save(Globals.pMRUfind);
MRU_Empty(Globals.pMRUreplace, false);
MRU_Empty(Globals.pMRUreplace, false, true);
MRU_Save(Globals.pMRUreplace);
} else {
MRU_MergeSave(Globals.pMRUfind, false, false, false);
@ -2473,16 +2476,55 @@ bool MRU_AddFile(LPMRULIST pmru, LPCWSTR pszFile, bool bRelativePath, bool bUnex
#pragma warning(push)
#pragma warning(disable : 6385 6386)
static void _MRU_DeleteItemInIniFile(LPCWSTR section, LPMRULIST pmru, const int iIndex)
{
bool bOpendByMe = false;
OpenSettingsFile(&bOpendByMe);
if (IsIniFileCached()) {
WCHAR wchName[32] = { L'\0' };
if (pmru->pszItems[iIndex]) {
StringCchPrintf(wchName, COUNTOF(wchName), L"%.2i", iIndex + 1);
IniSectionDelete(section, wchName, false);
if (pmru->iEncoding[iIndex] > 0) {
StringCchPrintf(wchName, COUNTOF(wchName), L"ENC%.2i", iIndex + 1);
IniSectionDelete(section, wchName, false);
}
if (pmru->iCaretPos[iIndex] >= 0) {
StringCchPrintf(wchName, COUNTOF(wchName), L"POS%.2i", iIndex + 1);
IniSectionDelete(section, wchName, false);
}
if (pmru->iSelAnchPos[iIndex] >= 0) {
StringCchPrintf(wchName, COUNTOF(wchName), L"ANC%.2i", iIndex + 1);
IniSectionDelete(section, wchName, false);
}
if (StrIsNotEmpty(pmru->pszBookMarks[iIndex])) {
StringCchPrintf(wchName, COUNTOF(wchName), L"BMRK%.2i", iIndex + 1);
IniSectionDelete(section, wchName, false);
}
}
CloseSettingsFile(false, bOpendByMe);
}
}
bool MRU_Delete(LPMRULIST pmru, int iIndex)
{
if (pmru && iIndex < MRU_MAXITEMS) {
if (iIndex >= 0 && iIndex < pmru->iSize) {
_MRU_DeleteItemInIniFile(pmru->szRegKey, pmru, iIndex);
if (pmru->pszItems[iIndex]) {
LocalFree(pmru->pszItems[iIndex]); // StrDup()
pmru->pszItems[iIndex] = NULL;
}
if (pmru->pszBookMarks[iIndex]) {
LocalFree(pmru->pszBookMarks[iIndex]); // StrDup()
LocalFree(pmru->pszBookMarks[iIndex]); // StrDup()
pmru->pszBookMarks[iIndex] = NULL;
}
bool bZeroMoved = false;
@ -2510,19 +2552,22 @@ bool MRU_Delete(LPMRULIST pmru, int iIndex)
#pragma warning(pop)
bool MRU_Empty(LPMRULIST pmru, bool bExceptLeast)
bool MRU_Empty(LPMRULIST pmru, bool bExceptLeast, bool bDelete)
{
if (pmru) {
int const beg = bExceptLeast ? 1 : 0;
for (int i = beg; i < pmru->iSize; ++i) {
if (pmru->pszItems[i]) {
LocalFree(pmru->pszItems[i]); // StrDup()
if (bDelete) {
_MRU_DeleteItemInIniFile(pmru->szRegKey, pmru, i);
}
LocalFree(pmru->pszItems[i]); // StrDup()
pmru->pszItems[i] = NULL;
pmru->iEncoding[i] = 0;
pmru->iCaretPos[i] = -1;
pmru->iSelAnchPos[i] = -1;
if (pmru->pszBookMarks[i]) {
LocalFree(pmru->pszBookMarks[i]); // StrDup()
LocalFree(pmru->pszBookMarks[i]); // StrDup()
pmru->pszBookMarks[i] = NULL;
}
}
@ -2562,7 +2607,7 @@ bool MRU_Load(LPMRULIST pmru, bool bFileProps)
int n = 0;
if (IsIniFileCached()) {
MRU_Empty(pmru, false);
MRU_Empty(pmru, false, false);
const WCHAR* const RegKey_Section = pmru->szRegKey;

View File

@ -133,7 +133,7 @@ bool IniFileIterateSection(const HPATHL hpthIniFile, LPCWSTR lpSectionName, Iter
//==== MRU Functions ==========================================================
void AddFilePathToRecentDocs(const HPATHL hpthIniFile);
void AddFilePathToRecentDocs(const HPATHL hpthFile);
//void ClearDestinationsOnRecentDocs();
LPMRULIST MRU_Create(LPCWSTR pszRegKey, int iFlags, int iSize);
@ -143,7 +143,7 @@ bool MRU_FindFile(LPMRULIST pmru, LPCWSTR pszFile, int* iIndex);
bool MRU_FindPath(LPMRULIST pmru, const HPATHL hpth, int* iIndex);
bool MRU_AddFile(LPMRULIST pmru, LPCWSTR pszFile, bool bRelativePath, bool bUnexpandMyDocs, cpi_enc_t iEnc, DocPos iPos, DocPos iSelAnc, LPCWSTR pszBookMarks);
bool MRU_Delete(LPMRULIST pmru, int iIndex);
bool MRU_Empty(LPMRULIST pmru, bool bExceptLeast);
bool MRU_Empty(LPMRULIST pmru, bool bExceptLeast, bool bDelete);
int MRU_Enum(LPMRULIST pmru, int iIndex, LPWSTR pszItem, int cchItem);
bool MRU_Load(LPMRULIST pmru, bool bFileProps);
void MRU_Save(LPMRULIST pmru);

View File

@ -2554,10 +2554,7 @@ CASE_WM_CTLCOLOR_SET:
case IDC_CLEAR_LIST:
ListView_DeleteAllItems(hwndLV);
MRU_Empty(Globals.pFileMRU, Path_IsNotEmpty(Paths.CurrentFile));
if (Globals.bCanSaveIniFile) {
MRU_Save(Globals.pFileMRU);
}
MRU_Empty(Globals.pFileMRU, Path_IsNotEmpty(Paths.CurrentFile), Globals.bCanSaveIniFile);
SendWMCommand(hwnd, IDC_FILEMRU_UPDATE_VIEW);
break;
@ -4325,7 +4322,7 @@ static INT_PTR CALLBACK AutoSaveBackupSettingsDlgProc(HWND hwnd, UINT umsg, WPAR
InfoBoxLng(MB_ICONINFORMATION, NULL, IDS_MUI_LOADFILE, Path_Get(hdir_pth));
// change dir
}
//if (GetFolderDlg(Globals.hwndMain, hdir_pth, Paths.WorkingDirectory, true)) {
//if (GetFolderDlg(Globals.hwndMain, hdir_pth, Paths.WorkingDirectory)) {
// InfoBoxLng(MB_ICONINFORMATION, NULL, IDS_MUI_LOADFILE, Path_Get(hdir_pth));
//}
Path_Release(hdir_pth);
@ -6582,7 +6579,7 @@ static void _CanonicalizeInitialDir(HPATHL hpth_in_out)
// lpstrInitialDir == NULL : leave initial dir to Open File Explorer
// lpstrInitialDir == ""[empty] : use a reasonable initial directory path
//
bool GetFolderDlg(HWND hwnd, HPATHL hdir_pth_io, const HPATHL hinidir_pth, bool bSelect)
bool GetFolderDlg(HWND hwnd, HPATHL hdir_pth_io, const HPATHL hinidir_pth)
{
if (!hdir_pth_io) {
return false;
@ -6591,20 +6588,19 @@ bool GetFolderDlg(HWND hwnd, HPATHL hdir_pth_io, const HPATHL hinidir_pth, bool
HPATHL hpth_dir = Path_Allocate(Path_Get(hinidir_pth));
_CanonicalizeInitialDir(hpth_dir);
LPCWSTR inidirBuf = NULL;
DWORD dwAttributes = Path_GetFileAttributes(hpth_dir);
if (bSelect || (dwAttributes == INVALID_FILE_ATTRIBUTES) || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
if ((dwAttributes == INVALID_FILE_ATTRIBUTES) || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
Path_RemoveFileSpec(hpth_dir);
}
if (bSelect && (dwAttributes != INVALID_FILE_ATTRIBUTES)) {
// if File is root, open the volume instead of open My Computer and select the volume
if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) && Path_IsRoot(hpth_dir)) {
bSelect = false;
}
else {
inidirBuf = Path_Get(hinidir_pth);
}
}
//if (dwAttributes != INVALID_FILE_ATTRIBUTES) {
// // if File is root, open the volume instead of open My Computer and select the volume
// if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) && Path_IsRoot(hpth_dir)) {
// bSelect = false;
// }
// else {
// inidirBuf = Path_Get(hinidir_pth);
// }
//}
dwAttributes = Path_GetFileAttributes(hpth_dir);
if ((dwAttributes == INVALID_FILE_ATTRIBUTES) || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
return false;
@ -6613,35 +6609,36 @@ bool GetFolderDlg(HWND hwnd, HPATHL hdir_pth_io, const HPATHL hinidir_pth, bool
Path_Swap(hdir_pth_io, hpth_dir);
Path_Release(hpth_dir);
LPCWSTR directory = Path_WriteAccessBuf(hdir_pth_io, PATHLONG_MAX_CCH);
HRESULT hr = S_FALSE;
PIDLIST_ABSOLUTE pidl = ILCreateFromPath(Path_WriteAccessBuf(hdir_pth_io, PATHLONG_MAX_CCH));
PIDLIST_ABSOLUTE pidl = ILCreateFromPath(directory);
if (pidl) {
PIDLIST_ABSOLUTE pidlEntry = inidirBuf ? ILCreateFromPath(inidirBuf) : NULL;
PIDLIST_ABSOLUTE pidlEntry = !Path_IsEmpty(hinidir_pth) ? ILCreateFromPath(Path_Get(hinidir_pth)) : NULL;
if (pidlEntry) {
hr = SHOpenFolderAndSelectItems(pidl, 1, (PCUITEMID_CHILD_ARRAY)(&pidlEntry), 0);
CoTaskMemFree((LPVOID)pidlEntry);
}
else if (!bSelect) {
#if 0
// Use an invalid item to open the folder?
hr = SHOpenFolderAndSelectItems(pidl, 1, (LPCITEMIDLIST *)(&pidl), 0);
#else
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
sei.fMask = SEE_MASK_IDLIST;
sei.hwnd = hwnd;
//~sei.lpVerb = L"explore";
sei.lpVerb = L"open";
sei.lpIDList = (void*)pidl;
sei.nShow = SW_SHOW;
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
sei.fMask = SEE_MASK_IDLIST | SEE_MASK_NOCLOSEPROCESS;
sei.hwnd = hwnd;
sei.lpVerb = L"explore";
//~sei.lpVerb = L"open";
sei.lpIDList = (void*)pidl;
sei.lpDirectory = NULL; //Path_Get(hinidir_pth);
sei.nShow = SW_SHOW;
sei.hInstApp = Globals.hInstance;
const BOOL result = ShellExecuteEx(&sei);
const BOOL result = ShellExecuteEx(&sei);
if (sei.hProcess) {
WaitForSingleObject(sei.hProcess, INFINITE);
CloseHandle(sei.hProcess);
hr = result ? S_OK : S_FALSE;
#endif
}
else {
// open parent folder and select the folder
hr = SHOpenFolderAndSelectItems(pidl, 0, NULL, 0);
hr = S_FALSE;
}
CoTaskMemFree((LPVOID)pidl);
}

View File

@ -279,7 +279,7 @@ INT_PTR ThemedDialogBoxParam(HINSTANCE hInstance, LPCTSTR lpTemplate, HWND hWndP
HWND CreateThemedDialogParam(HINSTANCE hInstance, LPCTSTR lpTemplate, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam);
void CleanupDlgResources();
//bool GetFolderDlg(HWND hwnd, HPATHL hdir_pth_io, const HPATHL hinidir_pth, bool bSelect);
//bool GetFolderDlg(HWND hwnd, HPATHL hdir_pth_io, const HPATHL hinidir_pth);
bool OpenFileDlg(HWND hwnd, HPATHL hfile_pth_io, const HPATHL hinidir_pth);
bool SaveFileDlg(HWND hwnd, HPATHL hfile_pth_io, const HPATHL hinidir_pth);

View File

@ -6828,10 +6828,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
break;
case IDACC_CLEAR_FIND_HISTORY:
MRU_Empty(Globals.pMRUfind, false);
if (Globals.bCanSaveIniFile) {
MRU_Save(Globals.pMRUfind);
}
MRU_Empty(Globals.pMRUfind, false, Globals.bCanSaveIniFile);
if (s_pEfrData) {
StrgEmpty(s_pEfrData->chFindPattern, false);
}
@ -6841,10 +6838,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
break;
case IDACC_CLEAR_REPL_HISTORY:
MRU_Empty(Globals.pMRUreplace, false);
if (Globals.bCanSaveIniFile) {
MRU_Save(Globals.pMRUreplace);
}
MRU_Empty(Globals.pMRUreplace, false, Globals.bCanSaveIniFile);
if (s_pEfrData) {
StrgEmpty(s_pEfrData->chReplaceTemplate, false);
}

View File

@ -660,7 +660,7 @@ static LPCWSTR _Path_SkipRoot(const HPATHL hpth)
//=============================================================================
//
// _Path_IsRelative()
// TODO: make LongPath version instead of slicing MAX_PATH
// TODO: §§§ make LongPath version instead of slicing MAX_PATH
//
static bool _Path_IsRelative(const HPATHL hpth)
{
@ -986,6 +986,9 @@ bool PTHAPI Path_RemoveFileSpec(HPATHL hpth_in_out)
}
StrgSanitize(hstr_io);
StrgTrimRight(hstr_io, L'\\');
StrgTrimRight(hstr_io, L'/');
return true;
}
// ----------------------------------------------------------------------------
@ -1736,7 +1739,7 @@ bool PTHAPI Path_CanonicalizeEx(HPATHL hpth_in_out, const HPATHL hdir_rel_base)
ExpandEnvironmentStrgs(hstr_io, true);
bool res = false;
if (_Path_IsRelative(hpth_in_out)) {
if (!Path_IsEmpty(hdir_rel_base) && _Path_IsRelative(hpth_in_out)) {
HPATHL hmod_pth = Path_Copy(hdir_rel_base);
Path_Append(hmod_pth, Path_Get(hpth_in_out));
res = Path_Canonicalize(hmod_pth);