From f06e9abd840dddb6deb92f688e76b7821f213ed8 Mon Sep 17 00:00:00 2001 From: "METANEOCORTEX\\Kotti" Date: Wed, 18 Nov 2026 17:07:38 +0100 Subject: [PATCH 1/2] Refresh and reopen Find/Replace on language changes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- agent-trail/domain/localization.md | 1 + src/Edit.c | 39 +++++++++++++++---- src/Edit.h | 2 +- src/MuiLanguage.c | 10 ++--- src/MuiLanguage.h | 2 +- src/Notepad3.c | 61 +++++++++++++++++++----------- 6 files changed, 79 insertions(+), 36 deletions(-) diff --git a/agent-trail/domain/localization.md b/agent-trail/domain/localization.md index 2fe4c36add..afc8687efb 100644 --- a/agent-trail/domain/localization.md +++ b/agent-trail/domain/localization.md @@ -6,6 +6,7 @@ There are 26 locales under `language\np3__\`; language packs buil ## Resource invariants +- Set the thread UI language before caching strings from `LoadMUILibrary`. Loading the module alone can leave `LoadStringW` resolving the previous language. Keep `SetMuiLanguage` before the status label loads in [`LoadLanguageResources`](../../src/MuiLanguage.c#L467-L486). A headless probe using the built language packs got `Z` when switching from German to French before `SetThreadUILanguage`, and `Lgn` after. - Every `IDS_MUI_*` in `language\common_res.h` must exist in all locale `strings_*.rc` files. A missing entry does not fail the build: `LoadLngStringW()` silently falls back to the built-in English string. - Every `IDS_LEX_*` must exist in the corresponding locale `lexer_*.rc` files. See [Lexer development](lexer-development.md) for style-label localization. - `.rc` files must be UTF-8 without BOM and use CRLF. Use `Build\rc_to_utf8.cmd` to strip accidental BOMs. In PowerShell use `[System.Text.UTF8Encoding]::new($false)`; in Python write `\r\n` explicitly. diff --git a/src/Edit.c b/src/Edit.c index f54181c884..66554cbc90 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -6799,6 +6799,11 @@ static void _AddPatternToComboMRU(HWND hwnd, int nIDDlgItem, LPCWSTR pszItem) // // EditFindReplaceDlgProc() // +typedef struct { + LPEDITFINDREPLACE pEfrData; + const POINT* pRestorePos; +} FindReplaceDlgInit; + extern int g_flagMatchText; // TODO: §§§ get rid of this buffer @@ -6835,9 +6840,8 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar switch (umsg) { case WM_INITDIALOG: { - // the global static Find/Replace data structure (main) - SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam); - s_pEfrData = (LPEDITFINDREPLACE)GetWindowLongPtr(hwnd, DWLP_USER); + const FindReplaceDlgInit* const pInit = (const FindReplaceDlgInit*)lParam; + s_pEfrData = pInit->pEfrData; Globals.hwndDlgFindReplace = hwnd; s_bIsReplaceDlg = (GetDlgItem(hwnd, IDC_REPLACETEXT) != NULL); @@ -6963,7 +6967,15 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar // Now set combobox text — CBN_EDITCHANGE will fire and call // _SetSearchFlags(), which can now read the correct checkbox states. - if (!GetWindowTextLengthW(GetDlgItem(hwnd, IDC_FINDTEXT))) { + if (pInit->pRestorePos) { + // Restore both fields before edit notifications can sync the partially initialized dialog. + s_pEfrData = NULL; + ComboBox_SetTextHW(hwnd, IDC_FINDTEXT, pInit->pEfrData->chFindPattern); + if (s_bIsReplaceDlg) { + ComboBox_SetTextHW(hwnd, IDC_REPLACETEXT, pInit->pEfrData->chReplaceTemplate); + } + s_pEfrData = pInit->pEfrData; + } else if (!GetWindowTextLengthW(GetDlgItem(hwnd, IDC_FINDTEXT))) { if (StrgIsNotEmpty(s_pEfrData->chFindPattern)) { ComboBox_SetTextHW(hwnd, IDC_FINDTEXT, s_pEfrData->chFindPattern); } @@ -6976,7 +6988,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar } } - if (s_bIsReplaceDlg) { + if (s_bIsReplaceDlg && !pInit->pRestorePos) { if (!GetWindowTextLengthW(GetDlgItem(hwnd, IDC_REPLACETEXT))) { if (StrgIsNotEmpty(s_pEfrData->chReplaceTemplate)) { ComboBox_SetTextHW(hwnd, IDC_REPLACETEXT, s_pEfrData->chReplaceTemplate); @@ -6996,6 +7008,9 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar SetDlgPos(hwnd, s_xFindReplaceDlgSave, s_yFindReplaceDlgSave); s_bSwitchedFindReplace = false; } + else if (pInit->pRestorePos) { + SetDlgPos(hwnd, pInit->pRestorePos->x, pInit->pRestorePos->y); + } else { if (Settings.FindReplaceDlgPosX == CW_USEDEFAULT || Settings.FindReplaceDlgPosY == CW_USEDEFAULT) { CenterDlgInParent(hwnd, false); @@ -7050,6 +7065,11 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar EnableWindow(Globals.hwndMain, (BOOL)wParam); return TRUE; + case WM_CLOSE: + // Language switching needs the modeless dialog destroyed before the resource DLL is unloaded. + DestroyWindow(hwnd); + return TRUE; + case WM_DESTROY: { KillTimer(hwnd, pTimerIdentifier); @@ -7799,17 +7819,22 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar // // EditFindReplaceDlg() // -HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace) +HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const POINT* pRestorePos) { lpefr->hwnd = hwnd; + FindReplaceDlgInit const init = { lpefr, pRestorePos }; HWND hDlg = CreateThemedDialogParam(Globals.hLngResContainer, (bReplace) ? MAKEINTRESOURCEW(IDD_MUI_REPLACE) : MAKEINTRESOURCEW(IDD_MUI_FIND), GetParent(hwnd), EditFindReplaceDlgProc, - (LPARAM) lpefr); + (LPARAM)&init); if (IS_VALID_HANDLE(hDlg)) { ShowWindow(hDlg, SW_SHOW); + if (pRestorePos) { + _EnableFRDlgCtrls(hDlg); + } + UpdateStatusbar(true); } return hDlg; } diff --git a/src/Edit.h b/src/Edit.h index fe4d4dee30..87dae95c5c 100644 --- a/src/Edit.h +++ b/src/Edit.h @@ -100,7 +100,7 @@ void EditFixPositions(); void EditEnsureConsistentLineEndings(HWND hwnd); void EditGetExcerpt(HWND hwnd,LPWSTR lpszExcerpt,DWORD cchExcerpt); -HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool); +HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const POINT* pRestorePos); bool EditFindNext(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool bExtendSelection, bool bFocusWnd, bool bFillEmpty); bool EditFindPrev(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool bExtendSelection, bool bFocusWnd, bool bFillEmpty); bool EditReplace(HWND hwnd, LPEDITFINDREPLACE lpefr); diff --git a/src/MuiLanguage.c b/src/MuiLanguage.c index 8d2b5529c0..66df24264b 100644 --- a/src/MuiLanguage.c +++ b/src/MuiLanguage.c @@ -375,9 +375,8 @@ bool GetUserPreferredLanguage(LPWSTR pszPrefLocaleName_out, int cchBuffer) //============================================================================= // // LoadLanguageResources -// return MUI_LanguageDLLs index // -unsigned LoadLanguageResources(LPCWSTR pLocaleName) { +void LoadLanguageResources(LPCWSTR pLocaleName) { FreeLanguageResources(); // reset Globals.hLngResContainer = NULL; // (!) unlink from instance resources @@ -464,6 +463,9 @@ unsigned LoadLanguageResources(LPCWSTR pLocaleName) { } } + // LoadString can still resolve the previous language until the thread UI language is set. + SetMuiLanguage(iLngIndex); + // === update language dependent items === for (cpi_enc_t enc = 0; enc < Encoding_CountOf(); ++enc) { @@ -484,8 +486,6 @@ unsigned LoadLanguageResources(LPCWSTR pLocaleName) { GetLngString(IDS_MUI_STATUSBAR_POSTFIXES, tchDefaultStrg, COUNTOF(tchDefaultStrg)); IniFileGetString(Paths.IniFile, StatusBar_Section, L"SectionPostfixes", tchDefaultStrg, tchStatusBar, COUNTOF(tchStatusBar)); ReadStrgsFromCSV(tchStatusBar, g_mxSBPostfix, STATUS_SECTOR_COUNT, MICRO_BUFFER, L"_POFX_"); - - return iLngIndex; } @@ -576,7 +576,7 @@ void DynamicLanguageMenuCmd(int cmd) { // desired language LPCWSTR desiredLocaleName = MUI_LanguageDLLs[iLngIdx].LocaleName; - SetMuiLanguage(LoadLanguageResources(desiredLocaleName)); + LoadLanguageResources(desiredLocaleName); Globals.hMainMenu = LoadMenu(Globals.hLngResContainer, MAKEINTRESOURCE(IDR_MUI_MAINMENU)); if (!Globals.hMainMenu) { diff --git a/src/MuiLanguage.h b/src/MuiLanguage.h index 6d01e789ec..e2b64279e7 100644 --- a/src/MuiLanguage.h +++ b/src/MuiLanguage.h @@ -113,7 +113,7 @@ inline bool IsSameLocale(const WCHAR *ln1, const WCHAR *ln2) { #if defined(HAVE_DYN_LOAD_LIBS_MUI_LNGS) bool GetUserPreferredLanguage(LPWSTR pszPrefLocaleName_out, int cchBuffer); -unsigned LoadLanguageResources(LPCWSTR localeName); +void LoadLanguageResources(LPCWSTR localeName); void FreeLanguageResources(); bool InsertLanguageMenu(HMENU hMenuBar); void DynamicLanguageMenuCmd(int cmd); diff --git a/src/Notepad3.c b/src/Notepad3.c index 74ee986714..2daa70a820 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -1417,7 +1417,7 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, // MultiLingual // #if defined(HAVE_DYN_LOAD_LIBS_MUI_LNGS) - SetMuiLanguage(LoadLanguageResources(Settings2.PreferredLanguageLocaleName)); + LoadLanguageResources(Settings2.PreferredLanguageLocaleName); #else SetMuiLanguage(GetMUILanguageIndexByLocaleName(MUI_BASE_LNG_ID)); #endif @@ -6955,13 +6955,13 @@ static bool _HandleEditFind(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) if (!IsWindow(Globals.hwndDlgFindReplace)) { // Don't overwrite cmd-line search pattern with editor selection Globals.bFindReplCopySelOrClip = !g_flagMatchText; - /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, false); + /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, false, NULL); } else { Globals.bFindReplCopySelOrClip = (GetForegroundWindow() != Globals.hwndDlgFindReplace); if (GetDlgItem(Globals.hwndDlgFindReplace, IDC_REPLACE)) { SendWMCommand(Globals.hwndDlgFindReplace, IDMSG_SWITCHTOFIND); DestroyWindow(Globals.hwndDlgFindReplace); - /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, false); + /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, false, NULL); } else { SetForegroundWindow(Globals.hwndDlgFindReplace); PostMessage(Globals.hwndDlgFindReplace, WM_NEXTDLGCTL, (WPARAM)(GetDlgItem(Globals.hwndDlgFindReplace, IDC_FINDTEXT)), 1); @@ -6976,13 +6976,13 @@ static bool _HandleEditFind(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) if (!IsWindow(Globals.hwndDlgFindReplace)) { // Don't overwrite cmd-line search pattern with editor selection Globals.bFindReplCopySelOrClip = !g_flagMatchText; - /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, true); + /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, true, NULL); } else { Globals.bFindReplCopySelOrClip = (GetForegroundWindow() != Globals.hwndDlgFindReplace); if (!GetDlgItem(Globals.hwndDlgFindReplace, IDC_REPLACE)) { SendWMCommand(Globals.hwndDlgFindReplace, IDMSG_SWITCHTOREPLACE); DestroyWindow(Globals.hwndDlgFindReplace); - /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, true); + /*Globals.hwndDlgFindReplace =*/ EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, true, NULL); } else { SetForegroundWindow(Globals.hwndDlgFindReplace); PostMessage(Globals.hwndDlgFindReplace, WM_NEXTDLGCTL, (WPARAM)(GetDlgItem(Globals.hwndDlgFindReplace, IDC_FINDTEXT)), 1); @@ -8701,10 +8701,25 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) #if defined(HAVE_DYN_LOAD_LIBS_MUI_LNGS) bool const bIsLngMenuCmd = ((iLoWParam >= IDS_MUI_LANG_EN_US) && (iLoWParam < (IDS_MUI_LANG_EN_US + MuiLanguages_CountOf()))); if (bIsLngMenuCmd) { + bool const bHadFindReplace = IsWindow(Globals.hwndDlgFindReplace); + bool bReplace = false; + int xDlg = 0; + int yDlg = 0; + if (bHadFindReplace) { + bReplace = GetDlgItem(Globals.hwndDlgFindReplace, IDC_REPLACE) != NULL; + GetDlgPos(Globals.hwndDlgFindReplace, &xDlg, &yDlg); + } + DynamicLanguageMenuCmd(iLoWParam); Style_InsertThemesMenu(Globals.hMainMenu); DrawMenuBar(Globals.hwndMain); + if (Globals.hMainMenu && bHadFindReplace && !IsWindow(Globals.hwndDlgFindReplace)) { + POINT const pos = { xDlg, yDlg }; + Globals.bFindReplCopySelOrClip = false; + EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, bReplace, &pos); + } UpdateToolbar(); + UpdateStatusbar(true); return FALSE; } #endif @@ -11559,7 +11574,9 @@ const static WCHAR *const FR_StatusW[] = { L"[>--<]", L"[>>--]", L"[>>-+]", L"[+ static void _UpdateStatusbarDelayed(bool bForceRedraw) { - if (!Settings.ShowStatusbar) { + bool const bShowStatusbar = Settings.ShowStatusbar; + bool const bIsWindowFindReplace = IsWindow(Globals.hwndDlgFindReplace); + if (!bShowStatusbar && !bIsWindowFindReplace) { return; } @@ -11578,8 +11595,6 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) bool const bIsSelectionEmpty = SciCall_IsSelectionEmpty(); bool const bIsMultiSelection = Sci_IsMultiOrRectangleSelection(); bool const bIsSelCharCountable = !bIsSelectionEmpty && !bIsMultiSelection; - bool const bIsWindowFindReplace = IsWindow(Globals.hwndDlgFindReplace); - bool bIsUpdateNeeded = bForceRedraw; static WCHAR tchLn[32] = { L'\0' }; @@ -11643,7 +11658,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) } // ------------------------------------------------------ - if (g_iStatusbarVisible[STATUS_DOCCHAR]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_DOCCHAR]) { static WCHAR tchChr[32] = { L'\0' }; static WCHAR tchChrs[32] = { L'\0' }; static DocPos s_iChr = -1; @@ -11673,7 +11688,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) } // ------------------------------------------------------ - if (g_iStatusbarVisible[STATUS_UNICODEPT]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_UNICODEPT]) { static WCHAR tchChr[32] = { L'\0' }; static UINT64 s_wChr = L'\0'; @@ -11697,7 +11712,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) else StringCchPrintf(tchChr, COUNTOF(tchChr), L"0x%.4X 0x%.4X", LOWORD(wChr), HIWORD(wChr)); } - if (s_wChr != wChr) { + if (bForceRedraw || (s_wChr != wChr)) { StringCchPrintf(tchStatusBar[STATUS_UNICODEPT], txtWidth, L"%s%s%s", g_mxSBPrefix[STATUS_UNICODEPT], tchChr, g_mxSBPostfix[STATUS_UNICODEPT]); s_wChr = wChr; @@ -11755,7 +11770,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) // ------------------------------------------------------ // number of selected lines in statusbar - if (g_iStatusbarVisible[STATUS_SELCTLINES]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_SELCTLINES]) { static bool s_bIsSelectionEmpty = true; static DocLn s_iLinesSelected = -1; @@ -11788,7 +11803,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) // ------------------------------------------------------ // try calculate expression of selection - if (g_iStatusbarVisible[STATUS_TINYEXPR]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_TINYEXPR]) { static WCHAR tchExpression[80] = { L'\0' }; // fits "0b" + 64 bits + NUL with headroom static te_int_t s_iExErr = -3; s_dExpression = 0.0; @@ -11885,7 +11900,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) // ------------------------------------------------------ // number of replaced pattern - if (g_iStatusbarVisible[STATUS_OCCREPLACE] || bIsWindowFindReplace) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_OCCREPLACE]) { static DocPosU s_iReplacedOccurrences = 0; if (bForceRedraw || (s_iReplacedOccurrences != Globals.iReplacedOccurrences)) { static WCHAR tchRepl[64] = { L'\0' }; @@ -11906,7 +11921,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) // ------------------------------------------------------ // get number of bytes in current encoding - if (g_iStatusbarVisible[STATUS_DOCSIZE]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_DOCSIZE]) { static DocPos s_iTextLength = -1; DocPos const iTextLength = SciCall_GetTextLength(); if (bForceRedraw || (s_iTextLength != iTextLength)) { @@ -11922,7 +11937,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) } // ------------------------------------------------------ - if (g_iStatusbarVisible[STATUS_CODEPAGE]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_CODEPAGE]) { static cpi_enc_t s_iEncoding = CPI_NONE; static bool s_bUTF8WithNul = false; cpi_enc_t const iEncoding = Encoding_GetCurrent(); @@ -11945,7 +11960,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) const WCHAR* const _CRLF_f = L"%sCR+LF%s"; const WCHAR* const _CRLFi_f = L"%sCR+LF*%s"; - if (g_iStatusbarVisible[STATUS_EOLMODE]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_EOLMODE]) { static int s_iEOLMode = -1; int const eol_mode = SciCall_GetEOLMode(); @@ -11967,7 +11982,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) } // ------------------------------------------------------ - if (g_iStatusbarVisible[STATUS_OVRMODE]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_OVRMODE]) { static int s_bIsOVR = -1; // int for -1 sentinel (first-paint trigger) static bool s_bPasteBoard = false; bool const bIsOVR = SciCall_GetOverType(); @@ -11989,7 +12004,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) } // ------------------------------------------------------ - if (g_iStatusbarVisible[STATUS_2ND_DEF]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_2ND_DEF]) { static bool s_bUse2ndDefault = true; bool const bUse2ndDefault = Style_GetUse2ndDefault(); if (bForceRedraw || (s_bUse2ndDefault != bUse2ndDefault)) { @@ -12006,7 +12021,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) } // ------------------------------------------------------ - if (g_iStatusbarVisible[STATUS_LEXER]) { + if (bShowStatusbar && g_iStatusbarVisible[STATUS_LEXER]) { static int s_iCurLexer = -1; int const iCurLexer = Style_GetCurrentLexerRID(); if (bForceRedraw || (s_iCurLexer != iCurLexer)) { @@ -12030,9 +12045,11 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) int g_vStatusbarSectionWidth[STATUS_SECTOR_COUNT] = SBS_INIT_MINUS; - _CalculateStatusbarSections(g_vStatusbarSectionWidth, tchStatusBar, &bIsUpdateNeeded); + if (bShowStatusbar) { + _CalculateStatusbarSections(g_vStatusbarSectionWidth, tchStatusBar, &bIsUpdateNeeded); + } - if (bIsUpdateNeeded) { + if (bShowStatusbar && bIsUpdateNeeded) { int aStatusbarSections[STATUS_SECTOR_COUNT] = SBS_INIT_ZERO; BYTE cnt = 0; int totalWidth = 0; From a7131c6ca40fb46c628d0106f87f611f3747865e Mon Sep 17 00:00:00 2001 From: "METANEOCORTEX\\Kotti" Date: Wed, 18 Nov 2026 17:34:40 +0100 Subject: [PATCH 2/2] Preserve Find/Replace view state across language switches Restore Focused View and dialog placement on recreation, and keep queued editor refreshes from clearing dialog-owned occurrence marks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../architecture/codebase-and-conventions.md | 4 ++ src/Edit.c | 71 ++++++++++++++----- src/Edit.h | 7 +- src/MuiLanguage.c | 32 ++++----- src/MuiLanguage.h | 2 +- src/Notepad3.c | 45 ++++++------ 6 files changed, 98 insertions(+), 63 deletions(-) diff --git a/agent-trail/architecture/codebase-and-conventions.md b/agent-trail/architecture/codebase-and-conventions.md index 416b6f8a66..017d249f50 100644 --- a/agent-trail/architecture/codebase-and-conventions.md +++ b/agent-trail/architecture/codebase-and-conventions.md @@ -83,6 +83,10 @@ Before an approved vendored-library change, consult the [Lexilla patch registry] - `ComboBox_SetText*` fires `CBN_EDITCHANGE` synchronously; reset one-shot flags before calling it. - `_FindInTarget` has an 8192-byte `chFind` buffer; a longer pattern makes `WideCharToMultiByte` fail (`len` = -1) and Scintilla reports "not found". +### Find/Replace dialog placement + +- For live recreation, capture the old screen rectangle and restore against its monitor. [`SetDlgPos`](../../src/Dialogs.c#L6157) is for owner-relative settings and clamps to the owner's monitor; [`_RestoreFindReplaceDlgPosition`](../../src/Edit.c#L6807) uses the old rectangle's monitor and fits the dialog again after any DPI resize. + ### Dynamic string/path buffer lifetime `StrgCreate(NULL)` can return a valid handle without allocating its text buffer; `StrgGet(h) == NULL` before initialization is normal, not proof of allocation failure. Initialize the fresh handle with `StrgReset(h, text)` and check its result (`> 0`) before using the text ([source](../../src/DynStrg.c#L432-L473): `StrgCreate`, `StrgReset`, `StrgGet`). diff --git a/src/Edit.c b/src/Edit.c index 66554cbc90..39c0376326 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -6801,9 +6801,32 @@ static void _AddPatternToComboMRU(HWND hwnd, int nIDDlgItem, LPCWSTR pszItem) // typedef struct { LPEDITFINDREPLACE pEfrData; - const POINT* pRestorePos; + const FindReplaceDlgRestore* pRestore; } FindReplaceDlgInit; +static void _RestoreFindReplaceDlgPosition(HWND hwnd, const RECT* pPreviousRect) +{ + MONITORINFO mi = { sizeof(mi) }; + if (IsRectEmpty(pPreviousRect) || + !GetMonitorInfo(MonitorFromRect(pPreviousRect, MONITOR_DEFAULTTONEAREST), &mi)) { + return; + } + + // The first move can resize the dialog via WM_DPICHANGED. Fit it again using the new size. + for (int i = 0; i < 2; ++i) { + RECT rc; + if (!GetWindowRect(hwnd, &rc)) { + return; + } + LONG const x = max_l(mi.rcWork.left, min_l(pPreviousRect->left, mi.rcWork.right - (rc.right - rc.left))); + LONG const y = max_l(mi.rcWork.top, min_l(pPreviousRect->top, mi.rcWork.bottom - (rc.bottom - rc.top))); + if (rc.left == x && rc.top == y) { + break; + } + SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); + } +} + extern int g_flagMatchText; // TODO: §§§ get rid of this buffer @@ -6816,6 +6839,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar static LPEDITFINDREPLACE s_pEfrData = NULL; static bool s_bIsReplaceDlg = false; + static bool s_bRestoreFocusedView = false; static UINT_PTR pTimerIdentifier = 0; @@ -6842,6 +6866,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar const FindReplaceDlgInit* const pInit = (const FindReplaceDlgInit*)lParam; s_pEfrData = pInit->pEfrData; + s_bRestoreFocusedView = pInit->pRestore && pInit->pRestore->bFocusedView; Globals.hwndDlgFindReplace = hwnd; s_bIsReplaceDlg = (GetDlgItem(hwnd, IDC_REPLACETEXT) != NULL); @@ -6967,7 +6992,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar // Now set combobox text — CBN_EDITCHANGE will fire and call // _SetSearchFlags(), which can now read the correct checkbox states. - if (pInit->pRestorePos) { + if (pInit->pRestore) { // Restore both fields before edit notifications can sync the partially initialized dialog. s_pEfrData = NULL; ComboBox_SetTextHW(hwnd, IDC_FINDTEXT, pInit->pEfrData->chFindPattern); @@ -6988,7 +7013,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar } } - if (s_bIsReplaceDlg && !pInit->pRestorePos) { + if (s_bIsReplaceDlg && !pInit->pRestore) { if (!GetWindowTextLengthW(GetDlgItem(hwnd, IDC_REPLACETEXT))) { if (StrgIsNotEmpty(s_pEfrData->chReplaceTemplate)) { ComboBox_SetTextHW(hwnd, IDC_REPLACETEXT, s_pEfrData->chReplaceTemplate); @@ -7008,8 +7033,8 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar SetDlgPos(hwnd, s_xFindReplaceDlgSave, s_yFindReplaceDlgSave); s_bSwitchedFindReplace = false; } - else if (pInit->pRestorePos) { - SetDlgPos(hwnd, pInit->pRestorePos->x, pInit->pRestorePos->y); + else if (pInit->pRestore) { + _RestoreFindReplaceDlgPosition(hwnd, &pInit->pRestore->windowRect); } else { if (Settings.FindReplaceDlgPosX == CW_USEDEFAULT || Settings.FindReplaceDlgPosY == CW_USEDEFAULT) { @@ -7394,19 +7419,26 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar break; case IDT_TIMER_CALLBACK_MRKALL: { - //DocPos const startPos = (DocPos)lParam; - s_anyMatch = _FindHasMatch(s_pEfrData->hwnd, s_pEfrData, 0, false); + bool const bRestoreFocusedView = s_bRestoreFocusedView; + s_bRestoreFocusedView = false; + static wchar_t s_lastFind[FNDRPL_BUFFER] = { L'\0' }; + bool const bRefreshMarks = s_pEfrData->bMarkOccurences && + (s_pEfrData->bStateChanged || (StringCchCompareX(s_lastFind, StrgGet(s_pEfrData->chFindPattern)) != 0)); + if (bRefreshMarks) { + StringCchCopyW(s_lastFind, COUNTOF(s_lastFind), StrgGet(s_pEfrData->chFindPattern)); + } + s_anyMatch = _FindHasMatch(s_pEfrData->hwnd, s_pEfrData, 0, bRefreshMarks); InvalidateRect(GetDlgItem(hwnd, IDC_FINDTEXT), NULL, TRUE); // coloring - if (s_pEfrData->bMarkOccurences) { - static wchar_t s_lastFind[FNDRPL_BUFFER] = { L'\0' }; - if (s_pEfrData->bStateChanged || (StringCchCompareX(s_lastFind, StrgGet(s_pEfrData->chFindPattern)) != 0)) { - StringCchCopyW(s_lastFind, COUNTOF(s_lastFind), StrgGet(s_pEfrData->chFindPattern)); - /*~s_anyMatch=~*/ _FindHasMatch(s_pEfrData->hwnd, s_pEfrData, 0, s_pEfrData->bMarkOccurences); - if (FocusedView.HideNonMatchedLines) { - EditToggleView(s_pEfrData->hwnd); - } + if (bRefreshMarks) { + if (bRestoreFocusedView && s_anyMatch == MATCH && (Settings.FocusViewMarkerMode & FVMM_FOLD)) { + // Fold the rebuilt matches without re-running Focused View's bookmark action. + FocusedView.HideNonMatchedLines = true; + EditFoldMarkedLineRange(s_pEfrData->hwnd, true); + UpdateDocumentReadOnlyState(); + } else if (FocusedView.HideNonMatchedLines) { + EditToggleView(s_pEfrData->hwnd); } - } else if (s_pEfrData->bStateChanged) { + } else if (!s_pEfrData->bMarkOccurences && s_pEfrData->bStateChanged) { if (FocusedView.HideNonMatchedLines) { SendWMCommand(hwnd, IDC_TOGGLE_VISIBILITY); } else { @@ -7442,6 +7474,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar case IDC_TOGGLE_VISIBILITY: if (s_pEfrData) { + s_bRestoreFocusedView = false; EditToggleView(s_pEfrData->hwnd); if (!FocusedView.HideNonMatchedLines) { s_pEfrData->bStateChanged = true; @@ -7819,10 +7852,10 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar // // EditFindReplaceDlg() // -HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const POINT* pRestorePos) +HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const FindReplaceDlgRestore* pRestore) { lpefr->hwnd = hwnd; - FindReplaceDlgInit const init = { lpefr, pRestorePos }; + FindReplaceDlgInit const init = { lpefr, pRestore }; HWND hDlg = CreateThemedDialogParam(Globals.hLngResContainer, (bReplace) ? MAKEINTRESOURCEW(IDD_MUI_REPLACE) : MAKEINTRESOURCEW(IDD_MUI_FIND), GetParent(hwnd), @@ -7831,7 +7864,7 @@ HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const if (IS_VALID_HANDLE(hDlg)) { ShowWindow(hDlg, SW_SHOW); - if (pRestorePos) { + if (pRestore) { _EnableFRDlgCtrls(hDlg); } UpdateStatusbar(true); diff --git a/src/Edit.h b/src/Edit.h index 87dae95c5c..38f6112da2 100644 --- a/src/Edit.h +++ b/src/Edit.h @@ -100,7 +100,12 @@ void EditFixPositions(); void EditEnsureConsistentLineEndings(HWND hwnd); void EditGetExcerpt(HWND hwnd,LPWSTR lpszExcerpt,DWORD cchExcerpt); -HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const POINT* pRestorePos); +typedef struct { + RECT windowRect; + bool bFocusedView; +} FindReplaceDlgRestore; + +HWND EditFindReplaceDlg(HWND hwnd, LPEDITFINDREPLACE lpefr, bool bReplace, const FindReplaceDlgRestore* pRestore); bool EditFindNext(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool bExtendSelection, bool bFocusWnd, bool bFillEmpty); bool EditFindPrev(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool bExtendSelection, bool bFocusWnd, bool bFillEmpty); bool EditReplace(HWND hwnd, LPEDITFINDREPLACE lpefr); diff --git a/src/MuiLanguage.c b/src/MuiLanguage.c index 66df24264b..2258f02d3a 100644 --- a/src/MuiLanguage.c +++ b/src/MuiLanguage.c @@ -562,33 +562,29 @@ bool InsertLanguageMenu(HMENU hMenuBar) { // // DynamicLanguageMenuCmd() - Handles IDS_MUI_LANG_XX_YY messages // -void DynamicLanguageMenuCmd(int cmd) { +bool DynamicLanguageMenuCmd(int cmd) { // consecutive resource IDs unsigned const iLngIdx = (unsigned)((cmd >= IDS_MUI_LANG_EN_US) ? (cmd - IDS_MUI_LANG_EN_US) : MuiLanguages_CountOf()); - if (iLngIdx >= MuiLanguages_CountOf()) { - return; + if (iLngIdx >= MuiLanguages_CountOf() || MUI_LanguageDLLs[iLngIdx].bIsActive) { + return false; } - if (!MUI_LanguageDLLs[iLngIdx].bIsActive) { - - DestroyMenu(Globals.hMainMenu); - // desired language - LPCWSTR desiredLocaleName = MUI_LanguageDLLs[iLngIdx].LocaleName; - LoadLanguageResources(desiredLocaleName); + DestroyMenu(Globals.hMainMenu); - Globals.hMainMenu = LoadMenu(Globals.hLngResContainer, MAKEINTRESOURCE(IDR_MUI_MAINMENU)); - if (!Globals.hMainMenu) { - InfoBoxLastError(L"LoadMenu()", 0); - CloseApplication(); - return; - } + LoadLanguageResources(MUI_LanguageDLLs[iLngIdx].LocaleName); - InsertLanguageMenu(Globals.hMainMenu); - SetMenu(Globals.hwndMain, (Settings.ShowMenubar ? Globals.hMainMenu : NULL)); - DrawMenuBar(Globals.hwndMain); + Globals.hMainMenu = LoadMenu(Globals.hLngResContainer, MAKEINTRESOURCE(IDR_MUI_MAINMENU)); + if (!Globals.hMainMenu) { + InfoBoxLastError(L"LoadMenu()", 0); + CloseApplication(); + return false; } + + InsertLanguageMenu(Globals.hMainMenu); + SetMenu(Globals.hwndMain, (Settings.ShowMenubar ? Globals.hMainMenu : NULL)); + return true; } diff --git a/src/MuiLanguage.h b/src/MuiLanguage.h index e2b64279e7..40555f41d7 100644 --- a/src/MuiLanguage.h +++ b/src/MuiLanguage.h @@ -116,7 +116,7 @@ bool GetUserPreferredLanguage(LPWSTR pszPrefLocaleName_out, int cchBuffer); void LoadLanguageResources(LPCWSTR localeName); void FreeLanguageResources(); bool InsertLanguageMenu(HMENU hMenuBar); -void DynamicLanguageMenuCmd(int cmd); +bool DynamicLanguageMenuCmd(int cmd); #endif // HAVE_DYN_LOAD_LIBS_MUI_LNGS diff --git a/src/Notepad3.c b/src/Notepad3.c index 2daa70a820..a73e4ecbaf 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -8703,20 +8703,21 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) if (bIsLngMenuCmd) { bool const bHadFindReplace = IsWindow(Globals.hwndDlgFindReplace); bool bReplace = false; - int xDlg = 0; - int yDlg = 0; + FindReplaceDlgRestore restore = { 0 }; if (bHadFindReplace) { bReplace = GetDlgItem(Globals.hwndDlgFindReplace, IDC_REPLACE) != NULL; - GetDlgPos(Globals.hwndDlgFindReplace, &xDlg, &yDlg); + GetWindowRect(Globals.hwndDlgFindReplace, &restore.windowRect); + restore.bFocusedView = FocusedView.HideNonMatchedLines; } - DynamicLanguageMenuCmd(iLoWParam); + if (!DynamicLanguageMenuCmd(iLoWParam)) { + return FALSE; + } Style_InsertThemesMenu(Globals.hMainMenu); DrawMenuBar(Globals.hwndMain); - if (Globals.hMainMenu && bHadFindReplace && !IsWindow(Globals.hwndDlgFindReplace)) { - POINT const pos = { xDlg, yDlg }; + if (bHadFindReplace && !IsWindow(Globals.hwndDlgFindReplace)) { Globals.bFindReplCopySelOrClip = false; - EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, bReplace, &pos); + EditFindReplaceDlg(Globals.hwndEdit, &s_FindReplaceData, bReplace, &restore); } UpdateToolbar(); UpdateStatusbar(true); @@ -8750,7 +8751,10 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) return FALSE; case IDT_TIMER_CALLBACK_MRKALL: - EditMarkAllOccurrences(Globals.hwndEdit, (bool)lParam); + // A queued editor refresh must not clear the open dialog's occurrence marks. + if (!IsWindow(Globals.hwndDlgFindReplace)) { + EditMarkAllOccurrences(Globals.hwndEdit, (bool)lParam); + } return FALSE; case IDT_TIMER_CLEAR_CALLTIP: @@ -11900,10 +11904,11 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) // ------------------------------------------------------ // number of replaced pattern - if (bShowStatusbar && g_iStatusbarVisible[STATUS_OCCREPLACE]) { + static WCHAR tchRepl[64] = L"--"; + bool const bShowReplacementCount = bShowStatusbar && g_iStatusbarVisible[STATUS_OCCREPLACE]; + if (bShowReplacementCount || bIsWindowFindReplace) { static DocPosU s_iReplacedOccurrences = 0; if (bForceRedraw || (s_iReplacedOccurrences != Globals.iReplacedOccurrences)) { - static WCHAR tchRepl[64] = { L'\0' }; if (Globals.iReplacedOccurrences) { StringCchPrintf(tchRepl, COUNTOF(tchRepl), DOCPOSFMTW, Globals.iReplacedOccurrences); FormatNumberStr(tchRepl, COUNTOF(tchRepl), 0); @@ -11911,11 +11916,12 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) StringCchCopy(tchRepl, COUNTOF(tchRepl), L"--"); } - StringCchPrintf(tchStatusBar[STATUS_OCCREPLACE], txtWidth, L"%s%s%s", - g_mxSBPrefix[STATUS_OCCREPLACE], tchRepl, g_mxSBPostfix[STATUS_OCCREPLACE]); - s_iReplacedOccurrences = Globals.iReplacedOccurrences; - bIsUpdateNeeded = true; + if (bShowReplacementCount) { + StringCchPrintf(tchStatusBar[STATUS_OCCREPLACE], txtWidth, L"%s%s%s", + g_mxSBPrefix[STATUS_OCCREPLACE], tchRepl, g_mxSBPostfix[STATUS_OCCREPLACE]); + bIsUpdateNeeded = true; + } } } // ------------------------------------------------------ @@ -12095,15 +12101,6 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) // update Find/Replace dialog (if any) if (bIsWindowFindReplace) { - static WCHAR tchReplOccs[64] = { L'\0' }; - if (Globals.iReplacedOccurrences) { - StringCchPrintf(tchReplOccs, COUNTOF(tchReplOccs), DOCPOSFMTW, Globals.iReplacedOccurrences); - FormatNumberStr(tchReplOccs, COUNTOF(tchReplOccs), 0); - } - else { - StringCchCopy(tchReplOccs, COUNTOF(tchReplOccs), L"--"); - } - const WCHAR* const SBFMT = L" %s%s / %s %s%s %s%s %s%s %s%s ( %s ) "; static WCHAR tchFRStatus[128] = { L'\0' }; @@ -12112,7 +12109,7 @@ static void _UpdateStatusbarDelayed(bool bForceRedraw) g_mxSBPrefix[STATUS_DOCCOLUMN], tchCol, g_mxSBPrefix[STATUS_SELECTION], tchSel, g_mxSBPrefix[STATUS_OCCURRENCE], tchOccOf, - g_mxSBPrefix[STATUS_OCCREPLACE], tchReplOccs, + g_mxSBPrefix[STATUS_OCCREPLACE], tchRepl, FR_StatusW[Globals.FindReplaceMatchFoundState]); SetWindowText(GetDlgItem(Globals.hwndDlgFindReplace, IDS_FR_STATUS_TEXT), tchFRStatus);