[NOTEPAD] Handle serialized maximized state (#5806)
authorWhindmar Saksit <whindsaks@proton.me>
Mon, 2 Sep 2024 21:13:28 +0000 (23:13 +0200)
committerGitHub <noreply@github.com>
Mon, 2 Sep 2024 21:13:28 +0000 (23:13 +0200)
Fixes a bug where if you close Notepad while it is maximized, the next time Notepad is started it will start with its window placed as if maximized but it is still in the SW_RESTORE state and the "real normal placement" is lost.

SetWindowPlacement also takes care of making sure the window is placed correctly on the monitor workarea.

base/applications/notepad/main.c
base/applications/notepad/notepad.h
base/applications/notepad/settings.c

index 85cd029..3668527 100644 (file)
@@ -557,10 +557,7 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE prev, LPTSTR cmdline, int sh
     MSG msg;
     HACCEL hAccel;
     WNDCLASSEX wndclass;
-    HMONITOR monitor;
-    MONITORINFO info;
-    INT x, y;
-    RECT rcIntersect;
+    WINDOWPLACEMENT wp;
     static const TCHAR className[] = _T("Notepad");
     static const TCHAR winName[] = _T("Notepad");
 
@@ -579,7 +576,7 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE prev, LPTSTR cmdline, int sh
     aFINDMSGSTRING = (ATOM)RegisterWindowMessage(FINDMSGSTRING);
 
     NOTEPAD_InitData(hInstance);
-    NOTEPAD_LoadSettingsFromRegistry();
+    NOTEPAD_LoadSettingsFromRegistry(&wp);
 
     ZeroMemory(&wndclass, sizeof(wndclass));
     wndclass.cbSize = sizeof(wndclass);
@@ -602,25 +599,14 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE prev, LPTSTR cmdline, int sh
         return 1;
     }
 
-    /* Setup windows */
-
-    monitor = MonitorFromRect(&Globals.main_rect, MONITOR_DEFAULTTOPRIMARY);
-    info.cbSize = sizeof(info);
-    GetMonitorInfoW(monitor, &info);
-
-    x = Globals.main_rect.left;
-    y = Globals.main_rect.top;
-    if (!IntersectRect(&rcIntersect, &Globals.main_rect, &info.rcWork))
-        x = y = CW_USEDEFAULT;
-
     /* Globals.hMainWnd will be set in WM_CREATE handling */
     CreateWindow(className,
                  winName,
                  WS_OVERLAPPEDWINDOW,
-                 x,
-                 y,
-                 Globals.main_rect.right - Globals.main_rect.left,
-                 Globals.main_rect.bottom - Globals.main_rect.top,
+                 CW_USEDEFAULT,
+                 CW_USEDEFAULT,
+                 CW_USEDEFAULT,
+                 CW_USEDEFAULT,
                  NULL,
                  NULL,
                  Globals.hInstance,
@@ -631,7 +617,17 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE prev, LPTSTR cmdline, int sh
         return 1;
     }
 
-    ShowWindow(Globals.hMainWnd, show);
+    /* Use the result of CW_USEDEFAULT if the data in the registry is not valid */
+    if (wp.rcNormalPosition.right == wp.rcNormalPosition.left)
+    {
+        GetWindowPlacement(Globals.hMainWnd, &wp);
+    }
+    /* Does the parent process want to force a show action? */
+    if (show != SW_SHOWDEFAULT)
+    {
+        wp.showCmd = show;
+    }
+    SetWindowPlacement(Globals.hMainWnd, &wp);
     UpdateWindow(Globals.hMainWnd);
 
     if (!HandleCommandLine(cmdline))
index 494fd8b..1e65ea1 100644 (file)
@@ -84,7 +84,6 @@ typedef struct
 
     FINDREPLACE find;
     WNDPROC EditProc;
-    RECT main_rect;
     BOOL bWasModified;
 } NOTEPAD_GLOBALS;
 
@@ -93,7 +92,7 @@ extern NOTEPAD_GLOBALS Globals;
 BOOL ReadText(HANDLE hFile, HLOCAL *phLocal, ENCODING *pencFile, EOLN *piEoln);
 BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile, EOLN iEoln);
 
-void NOTEPAD_LoadSettingsFromRegistry(void);
+void NOTEPAD_LoadSettingsFromRegistry(PWINDOWPLACEMENT pWP);
 void NOTEPAD_SaveSettingsToRegistry(void);
 
 BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bReplace, BOOL bShowAlert);
index 6c0c167..24ddf3f 100644 (file)
@@ -102,12 +102,12 @@ static BOOL QueryString(HKEY hKey, LPCTSTR pszValueName, LPTSTR pszResult, DWORD
  *
  *  Load settings from registry HKCU\Software\Microsoft\Notepad.
  */
-void NOTEPAD_LoadSettingsFromRegistry(void)
+void NOTEPAD_LoadSettingsFromRegistry(PWINDOWPLACEMENT pWP)
 {
     HKEY hKey;
     HFONT hFont;
-    DWORD dwPointSize, cx, cy;
-    DWORD cxScreen = GetSystemMetrics(SM_CXSCREEN), cyScreen = GetSystemMetrics(SM_CYSCREEN);
+    DWORD dwPointSize;
+    DWORD x = CW_USEDEFAULT, y = CW_USEDEFAULT, cx = 0, cy = 0;
 
     /* Set the default values */
     Globals.bShowStatusBar = TRUE;
@@ -118,10 +118,6 @@ void NOTEPAD_LoadSettingsFromRegistry(void)
     dwPointSize = 100;
     Globals.lfFont.lfWeight = FW_NORMAL;
     Globals.lfFont.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
-    Globals.main_rect.left = CW_USEDEFAULT;
-    Globals.main_rect.top = CW_USEDEFAULT;
-    cx = min((cxScreen * 3) / 4, 640);
-    cy = min((cyScreen * 3) / 4, 480);
 
     /* FIXME: Globals.fSaveWindowPositions = FALSE; */
     /* FIXME: Globals.fMLE_is_broken = FALSE; */
@@ -154,8 +150,8 @@ void NOTEPAD_LoadSettingsFromRegistry(void)
         QueryDword(hKey, _T("iMarginRight"), (DWORD*)&Globals.lMargins.right);
         QueryDword(hKey, _T("iMarginBottom"), (DWORD*)&Globals.lMargins.bottom);
 
-        QueryDword(hKey, _T("iWindowPosX"), (DWORD*)&Globals.main_rect.left);
-        QueryDword(hKey, _T("iWindowPosY"), (DWORD*)&Globals.main_rect.top);
+        QueryDword(hKey, _T("iWindowPosX"), &x);
+        QueryDword(hKey, _T("iWindowPosY"), &y);
         QueryDword(hKey, _T("iWindowPosDX"), &cx);
         QueryDword(hKey, _T("iWindowPosDY"), &cy);
 
@@ -163,10 +159,23 @@ void NOTEPAD_LoadSettingsFromRegistry(void)
         QueryString(hKey, _T("replaceString"), Globals.szReplaceText, _countof(Globals.szReplaceText));
     }
 
-    Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
-    Globals.main_rect.right = Globals.main_rect.left + cx;
-    Globals.main_rect.bottom = Globals.main_rect.top + cy;
+    pWP->length = sizeof(*pWP);
+    pWP->flags = 0;
+    pWP->showCmd = SW_SHOWDEFAULT;
+    if (cy & 0x80000000)
+    {
+        cy &= ~0x80000000;
+        pWP->flags |= WPF_RESTORETOMAXIMIZED;
+        pWP->showCmd = SW_SHOWMAXIMIZED;
+    }
+    pWP->rcNormalPosition.left = x;
+    pWP->rcNormalPosition.right = x + cx;
+    pWP->rcNormalPosition.top = y;
+    pWP->rcNormalPosition.bottom = y + cy;
+    pWP->ptMaxPosition.x = x;
+    pWP->ptMaxPosition.y = y;
 
+    Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
     if (!hKey || !QueryString(hKey, _T("lfFaceName"),
                               Globals.lfFont.lfFaceName, _countof(Globals.lfFont.lfFaceName)))
     {
@@ -228,8 +237,18 @@ void NOTEPAD_SaveSettingsToRegistry(void)
 {
     HKEY hKey;
     DWORD dwDisposition;
+    WINDOWPLACEMENT wp;
+    UINT x, y, cx, cy;
+
+    wp.length = sizeof(wp);
+    GetWindowPlacement(Globals.hMainWnd, &wp);
+    x = wp.rcNormalPosition.left;
+    y = wp.rcNormalPosition.top;
+    cx = wp.rcNormalPosition.right - x;
+    cy = wp.rcNormalPosition.bottom - y;
+    if (wp.flags & WPF_RESTORETOMAXIMIZED)
+        cy |= 0x80000000;
 
-    GetWindowRect(Globals.hMainWnd, &Globals.main_rect);
 
     if (RegCreateKeyEx(HKEY_CURRENT_USER, s_szRegistryKey,
                        0, NULL, 0, KEY_SET_VALUE, NULL,
@@ -256,10 +275,10 @@ void NOTEPAD_SaveSettingsToRegistry(void)
         SaveDword(hKey, _T("iMarginTop"), Globals.lMargins.top);
         SaveDword(hKey, _T("iMarginRight"), Globals.lMargins.right);
         SaveDword(hKey, _T("iMarginBottom"), Globals.lMargins.bottom);
-        SaveDword(hKey, _T("iWindowPosX"), Globals.main_rect.left);
-        SaveDword(hKey, _T("iWindowPosY"), Globals.main_rect.top);
-        SaveDword(hKey, _T("iWindowPosDX"), Globals.main_rect.right - Globals.main_rect.left);
-        SaveDword(hKey, _T("iWindowPosDY"), Globals.main_rect.bottom - Globals.main_rect.top);
+        SaveDword(hKey, _T("iWindowPosX"), x);
+        SaveDword(hKey, _T("iWindowPosY"), y);
+        SaveDword(hKey, _T("iWindowPosDX"), cx);
+        SaveDword(hKey, _T("iWindowPosDY"), cy);
         SaveString(hKey, _T("searchString"), Globals.szFindText);
         SaveString(hKey, _T("replaceString"), Globals.szReplaceText);