[MSPAINT] Refactor about Recent Files (#5163)

Define MAX_RECENT_FILES macro as 4.
Remove strFile1, ..., strFile4 settings and add strFiles[MAX_RECENT_FILES] for Most Recently Used (MRU) files.
CORE-18867
This commit is contained in:
Katayama Hirofumi MZ 2023-03-17 07:28:01 +09:00 committed by GitHub
parent a81f229065
commit 41c30182d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 95 deletions

View file

@ -115,10 +115,12 @@ void RegistrySettings::Load(INT nCmdShow)
CRegKey files; CRegKey files;
if (files.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS) if (files.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS)
{ {
ReadString(files, _T("File1"), strFile1); TCHAR szName[64];
ReadString(files, _T("File2"), strFile2); for (INT i = 0; i < MAX_RECENT_FILES; ++i)
ReadString(files, _T("File3"), strFile3); {
ReadString(files, _T("File4"), strFile4); wsprintf(szName, _T("File%u"), i + 1);
ReadString(files, szName, strFiles[i]);
}
} }
CRegKey text; CRegKey text;
@ -167,14 +169,12 @@ void RegistrySettings::Store()
CRegKey files; CRegKey files;
if (files.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS) if (files.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
{ {
if (!strFile1.IsEmpty()) TCHAR szName[64];
files.SetStringValue(_T("File1"), strFile1); for (INT iFile = 0; iFile < MAX_RECENT_FILES; ++iFile)
if (!strFile2.IsEmpty()) {
files.SetStringValue(_T("File2"), strFile2); wsprintf(szName, _T("File%u"), iFile + 1);
if (!strFile3.IsEmpty()) files.SetStringValue(szName, strFiles[iFile]);
files.SetStringValue(_T("File3"), strFile3); }
if (!strFile4.IsEmpty())
files.SetStringValue(_T("File4"), strFile4);
} }
CRegKey text; CRegKey text;
@ -194,39 +194,30 @@ void RegistrySettings::Store()
void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName) void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName)
{ {
// Register the file to the user's 'Recent' folder
if (szPathName && szPathName[0]) if (szPathName && szPathName[0])
SHAddToRecentDocs(SHARD_PATHW, szPathName); SHAddToRecentDocs(SHARD_PATHW, szPathName);
if (strFile1 == szPathName) // If szPathName is present in strFiles, move it to the top of the list
for (INT i = MAX_RECENT_FILES - 1, iFound = -1; i > 0; --i)
{ {
// do nothing if (iFound < 0 && strFiles[i].CompareNoCase(szPathName) == 0)
iFound = i;
if (iFound >= 0)
{
CString tmp = strFiles[i];
strFiles[i] = strFiles[i - 1];
strFiles[i - 1] = tmp;
}
} }
else if (strFile2 == szPathName)
// If szPathName is not the first item in strFiles, insert it at the top of the list
if (strFiles[0].CompareNoCase(szPathName) != 0)
{ {
CString strTemp = strFile2; for (INT i = MAX_RECENT_FILES - 1; i > 0; --i)
strFile2 = strFile1; strFiles[i] = strFiles[i - 1];
strFile1 = strTemp;
} strFiles[0] = szPathName;
else if (strFile3 == szPathName)
{
CString strTemp = strFile3;
strFile3 = strFile2;
strFile2 = strFile1;
strFile1 = strTemp;
}
else if (strFile4 == szPathName)
{
CString strTemp = strFile4;
strFile4 = strFile3;
strFile3 = strFile2;
strFile2 = strFile1;
strFile1 = strTemp;
}
else
{
strFile4 = strFile3;
strFile3 = strFile2;
strFile2 = strFile1;
strFile1 = szPathName;
} }
} }

View file

@ -8,6 +8,8 @@
#pragma once #pragma once
#define MAX_RECENT_FILES 4
class RegistrySettings class RegistrySettings
{ {
private: private:
@ -27,10 +29,7 @@ public:
DWORD UnitSetting; DWORD UnitSetting;
WINDOWPLACEMENT WindowPlacement; WINDOWPLACEMENT WindowPlacement;
CString strFile1; CString strFiles[MAX_RECENT_FILES];
CString strFile2;
CString strFile3;
CString strFile4;
CString strFontName; CString strFontName;
DWORD PointSize; DWORD PointSize;

View file

@ -10,6 +10,7 @@
*/ */
#include "precomp.h" #include "precomp.h"
#include <assert.h>
typedef HWND (WINAPI *FN_HtmlHelpW)(HWND, LPCWSTR, UINT, DWORD_PTR); typedef HWND (WINAPI *FN_HtmlHelpW)(HWND, LPCWSTR, UINT, DWORD_PTR);
@ -310,53 +311,63 @@ LRESULT CMainWindow::OnClose(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHan
return 0; return 0;
} }
void CMainWindow::ProcessFileMenu(HMENU hPopupMenu)
{
LPCTSTR dotext = PathFindExtensionW(filepathname);
BOOL isBMP = FALSE;
if (_tcsicmp(dotext, _T(".bmp")) == 0 ||
_tcsicmp(dotext, _T(".dib")) == 0 ||
_tcsicmp(dotext, _T(".rle")) == 0)
{
isBMP = TRUE;
}
EnableMenuItem(hPopupMenu, IDM_FILEASWALLPAPERPLANE, ENABLED_IF(isAFile && isBMP));
EnableMenuItem(hPopupMenu, IDM_FILEASWALLPAPERCENTERED, ENABLED_IF(isAFile && isBMP));
EnableMenuItem(hPopupMenu, IDM_FILEASWALLPAPERSTRETCHED, ENABLED_IF(isAFile && isBMP));
for (INT iItem = 0; iItem < MAX_RECENT_FILES; ++iItem)
RemoveMenu(hPopupMenu, IDM_FILE1 + iItem, MF_BYCOMMAND);
if (registrySettings.strFiles[0].IsEmpty())
return;
RemoveMenu(hPopupMenu, IDM_FILEMOSTRECENTLYUSEDFILE, MF_BYCOMMAND);
INT cMenuItems = GetMenuItemCount(hPopupMenu);
for (INT iItem = 0; iItem < MAX_RECENT_FILES; ++iItem)
{
CString& strFile = registrySettings.strFiles[iItem];
if (strFile.IsEmpty())
break;
// Condense the lengthy pathname by using '...'
#define MAX_RECENT_PATHNAME_DISPLAY 30
CPath pathFile(strFile);
pathFile.CompactPathEx(MAX_RECENT_PATHNAME_DISPLAY);
assert(_tcslen((LPCTSTR)pathFile) <= MAX_RECENT_PATHNAME_DISPLAY);
// Add an accelerator (by '&') to the item number for quick access
TCHAR szText[4 + MAX_RECENT_PATHNAME_DISPLAY + 1];
wsprintf(szText, _T("&%u %s"), iItem + 1, (LPCTSTR)pathFile);
INT iMenuItem = (cMenuItems - 2) + iItem;
InsertMenu(hPopupMenu, iMenuItem, MF_BYPOSITION | MF_STRING, IDM_FILE1 + iItem, szText);
}
}
LRESULT CMainWindow::OnInitMenuPopup(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) LRESULT CMainWindow::OnInitMenuPopup(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{ {
HMENU menu = GetMenu(); HMENU menu = GetMenu();
BOOL trueSelection = BOOL trueSelection =
(::IsWindowVisible(selectionWindow) && (::IsWindowVisible(selectionWindow) &&
((toolsModel.GetActiveTool() == TOOL_FREESEL) || (toolsModel.GetActiveTool() == TOOL_RECTSEL))); ((toolsModel.GetActiveTool() == TOOL_FREESEL) || (toolsModel.GetActiveTool() == TOOL_RECTSEL)));
BOOL isBMP;
switch (lParam) switch (lParam)
{ {
case 0: /* File menu */ case 0: /* File menu */
if ((HMENU)wParam != GetSubMenu(menu, 0)) ProcessFileMenu((HMENU)wParam);
break;
isBMP = _wcsicmp(PathFindExtensionW(filepathname), L".bmp") == 0;
EnableMenuItem(menu, IDM_FILEASWALLPAPERPLANE, ENABLED_IF(isAFile && isBMP));
EnableMenuItem(menu, IDM_FILEASWALLPAPERCENTERED, ENABLED_IF(isAFile && isBMP));
EnableMenuItem(menu, IDM_FILEASWALLPAPERSTRETCHED, ENABLED_IF(isAFile && isBMP));
RemoveMenu(menu, IDM_FILE1, MF_BYCOMMAND);
RemoveMenu(menu, IDM_FILE2, MF_BYCOMMAND);
RemoveMenu(menu, IDM_FILE3, MF_BYCOMMAND);
RemoveMenu(menu, IDM_FILE4, MF_BYCOMMAND);
if (!registrySettings.strFile1.IsEmpty())
{
RemoveMenu(menu, IDM_FILEMOSTRECENTLYUSEDFILE, MF_BYCOMMAND);
CPath pathFile1(registrySettings.strFile1);
pathFile1.CompactPathEx(30);
if (!registrySettings.strFile2.IsEmpty())
{
CPath pathFile2(registrySettings.strFile2);
pathFile2.CompactPathEx(30);
if (!registrySettings.strFile3.IsEmpty())
{
CPath pathFile3(registrySettings.strFile3);
pathFile3.CompactPathEx(30);
if (!registrySettings.strFile4.IsEmpty())
{
CPath pathFile4(registrySettings.strFile4);
pathFile4.CompactPathEx(30);
InsertMenu((HMENU)wParam, 17, MF_BYPOSITION | MF_STRING, IDM_FILE4, _T("4 ") + pathFile4);
}
InsertMenu((HMENU)wParam, 17, MF_BYPOSITION | MF_STRING, IDM_FILE3, _T("3 ") + pathFile3);
}
InsertMenu((HMENU)wParam, 17, MF_BYPOSITION | MF_STRING, IDM_FILE2, _T("2 ") + pathFile2);
}
InsertMenu((HMENU)wParam, 17, MF_BYPOSITION | MF_STRING, IDM_FILE1, _T("1 ") + pathFile1);
}
break; break;
case 1: /* Edit menu */ case 1: /* Edit menu */
EnableMenuItem(menu, IDM_EDITUNDO, ENABLED_IF(imageModel.HasUndoSteps())); EnableMenuItem(menu, IDM_EDITUNDO, ENABLED_IF(imageModel.HasUndoSteps()));
@ -545,23 +556,13 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
RegistrySettings::SetWallpaper(filepathname, RegistrySettings::STRETCHED); RegistrySettings::SetWallpaper(filepathname, RegistrySettings::STRETCHED);
break; break;
case IDM_FILE1: case IDM_FILE1:
{
ConfirmSave() && DoLoadImageFile(m_hWnd, registrySettings.strFile1, TRUE);
break;
}
case IDM_FILE2: case IDM_FILE2:
{
ConfirmSave() && DoLoadImageFile(m_hWnd, registrySettings.strFile2, TRUE);
break;
}
case IDM_FILE3: case IDM_FILE3:
{
ConfirmSave() && DoLoadImageFile(m_hWnd, registrySettings.strFile3, TRUE);
break;
}
case IDM_FILE4: case IDM_FILE4:
{ {
ConfirmSave() && DoLoadImageFile(m_hWnd, registrySettings.strFile4, TRUE); INT iFile = LOWORD(wParam) - IDM_FILE1;
if (ConfirmSave())
DoLoadImageFile(m_hWnd, registrySettings.strFiles[iFile], TRUE);
break; break;
} }
case IDM_EDITUNDO: case IDM_EDITUNDO:

View file

@ -28,6 +28,7 @@ public:
MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel) MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel)
END_MSG_MAP() END_MSG_MAP()
private:
LRESULT OnDropFiles(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnDropFiles(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled); LRESULT OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
@ -44,4 +45,5 @@ public:
void saveImage(BOOL overwrite); void saveImage(BOOL overwrite);
void InsertSelectionFromHBITMAP(HBITMAP bitmap, HWND window); void InsertSelectionFromHBITMAP(HBITMAP bitmap, HWND window);
BOOL ConfirmSave(); BOOL ConfirmSave();
void ProcessFileMenu(HMENU hPopupMenu);
}; };