https://git.reactos.org/?p=reactos.git;a=commitdiff;h=41c30182d4638d270b493dee631946aaec5d8b51

commit 41c30182d4638d270b493dee631946aaec5d8b51
Author:     Katayama Hirofumi MZ <[email protected]>
AuthorDate: Fri Mar 17 07:28:01 2023 +0900
Commit:     GitHub <[email protected]>
CommitDate: Fri Mar 17 07:28:01 2023 +0900

    [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
---
 base/applications/mspaint/registry.cpp |  71 ++++++++++-------------
 base/applications/mspaint/registry.h   |   7 +--
 base/applications/mspaint/winproc.cpp  | 103 +++++++++++++++++----------------
 base/applications/mspaint/winproc.h    |   2 +
 4 files changed, 88 insertions(+), 95 deletions(-)

diff --git a/base/applications/mspaint/registry.cpp 
b/base/applications/mspaint/registry.cpp
index c027fcac0e7..46119989429 100644
--- a/base/applications/mspaint/registry.cpp
+++ b/base/applications/mspaint/registry.cpp
@@ -115,10 +115,12 @@ void RegistrySettings::Load(INT nCmdShow)
     CRegKey files;
     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);
-        ReadString(files, _T("File2"), strFile2);
-        ReadString(files, _T("File3"), strFile3);
-        ReadString(files, _T("File4"), strFile4);
+        TCHAR szName[64];
+        for (INT i = 0; i < MAX_RECENT_FILES; ++i)
+        {
+            wsprintf(szName, _T("File%u"), i + 1);
+            ReadString(files, szName, strFiles[i]);
+        }
     }
 
     CRegKey text;
@@ -167,14 +169,12 @@ void RegistrySettings::Store()
     CRegKey files;
     if (files.Create(HKEY_CURRENT_USER, 
_T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File 
List")) == ERROR_SUCCESS)
     {
-        if (!strFile1.IsEmpty())
-            files.SetStringValue(_T("File1"), strFile1);
-        if (!strFile2.IsEmpty())
-            files.SetStringValue(_T("File2"), strFile2);
-        if (!strFile3.IsEmpty())
-            files.SetStringValue(_T("File3"), strFile3);
-        if (!strFile4.IsEmpty())
-            files.SetStringValue(_T("File4"), strFile4);
+        TCHAR szName[64];
+        for (INT iFile = 0; iFile < MAX_RECENT_FILES; ++iFile)
+        {
+            wsprintf(szName, _T("File%u"), iFile + 1);
+            files.SetStringValue(szName, strFiles[iFile]);
+        }
     }
 
     CRegKey text;
@@ -194,39 +194,30 @@ void RegistrySettings::Store()
 
 void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName)
 {
+    // Register the file to the user's 'Recent' folder
     if (szPathName && szPathName[0])
         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)
-    {
-        CString strTemp = strFile2;
-        strFile2 = strFile1;
-        strFile1 = strTemp;
-    }
-    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
+
+    // If szPathName is not the first item in strFiles, insert it at the top 
of the list
+    if (strFiles[0].CompareNoCase(szPathName) != 0)
     {
-        strFile4 = strFile3;
-        strFile3 = strFile2;
-        strFile2 = strFile1;
-        strFile1 = szPathName;
+        for (INT i = MAX_RECENT_FILES - 1; i > 0; --i)
+            strFiles[i] = strFiles[i - 1];
+
+        strFiles[0] = szPathName;
     }
 }
diff --git a/base/applications/mspaint/registry.h 
b/base/applications/mspaint/registry.h
index e7b4cc644ee..b891d523c23 100644
--- a/base/applications/mspaint/registry.h
+++ b/base/applications/mspaint/registry.h
@@ -8,6 +8,8 @@
 
 #pragma once
 
+#define MAX_RECENT_FILES 4
+
 class RegistrySettings
 {
 private:
@@ -27,10 +29,7 @@ public:
     DWORD UnitSetting;
     WINDOWPLACEMENT WindowPlacement;
 
-    CString strFile1;
-    CString strFile2;
-    CString strFile3;
-    CString strFile4;
+    CString strFiles[MAX_RECENT_FILES];
 
     CString strFontName;
     DWORD PointSize;
diff --git a/base/applications/mspaint/winproc.cpp 
b/base/applications/mspaint/winproc.cpp
index ac96bb2cfcd..e6669e19e68 100644
--- a/base/applications/mspaint/winproc.cpp
+++ b/base/applications/mspaint/winproc.cpp
@@ -10,6 +10,7 @@
  */
 
 #include "precomp.h"
+#include <assert.h>
 
 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;
 }
 
+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)
 {
     HMENU menu = GetMenu();
     BOOL trueSelection =
         (::IsWindowVisible(selectionWindow) &&
          ((toolsModel.GetActiveTool() == TOOL_FREESEL) || 
(toolsModel.GetActiveTool() == TOOL_RECTSEL)));
-    BOOL isBMP;
+
     switch (lParam)
     {
         case 0: /* File menu */
-            if ((HMENU)wParam != GetSubMenu(menu, 0))
-                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);
-            }
+            ProcessFileMenu((HMENU)wParam);
             break;
         case 1: /* Edit menu */
             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);
             break;
         case IDM_FILE1:
-        {
-            ConfirmSave() && DoLoadImageFile(m_hWnd, 
registrySettings.strFile1, TRUE);
-            break;
-        }
         case IDM_FILE2:
-        {
-            ConfirmSave() && DoLoadImageFile(m_hWnd, 
registrySettings.strFile2, TRUE);
-            break;
-        }
         case IDM_FILE3:
-        {
-            ConfirmSave() && DoLoadImageFile(m_hWnd, 
registrySettings.strFile3, TRUE);
-            break;
-        }
         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;
         }
         case IDM_EDITUNDO:
diff --git a/base/applications/mspaint/winproc.h 
b/base/applications/mspaint/winproc.h
index 749277560a3..71db5e7950d 100644
--- a/base/applications/mspaint/winproc.h
+++ b/base/applications/mspaint/winproc.h
@@ -28,6 +28,7 @@ public:
         MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel)
     END_MSG_MAP()
 
+private:
     LRESULT OnDropFiles(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);
@@ -44,4 +45,5 @@ public:
     void saveImage(BOOL overwrite);
     void InsertSelectionFromHBITMAP(HBITMAP bitmap, HWND window);
     BOOL ConfirmSave();
+    void ProcessFileMenu(HMENU hPopupMenu);
 };

Reply via email to