Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions agent-trail/architecture/codebase-and-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
1 change: 1 addition & 0 deletions agent-trail/domain/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ There are 26 locales under `language\np3_<LANG>_<COUNTRY>\`; 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.
Expand Down
94 changes: 76 additions & 18 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6799,6 +6799,34 @@ static void _AddPatternToComboMRU(HWND hwnd, int nIDDlgItem, LPCWSTR pszItem)
//
// EditFindReplaceDlgProc()
//
typedef struct {
LPEDITFINDREPLACE pEfrData;
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
Expand All @@ -6811,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;

Expand All @@ -6835,9 +6864,9 @@ 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;
s_bRestoreFocusedView = pInit->pRestore && pInit->pRestore->bFocusedView;

Globals.hwndDlgFindReplace = hwnd;
s_bIsReplaceDlg = (GetDlgItem(hwnd, IDC_REPLACETEXT) != NULL);
Expand Down Expand Up @@ -6963,7 +6992,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->pRestore) {
// 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);
}
Expand All @@ -6976,7 +7013,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
}
}

if (s_bIsReplaceDlg) {
if (s_bIsReplaceDlg && !pInit->pRestore) {
if (!GetWindowTextLengthW(GetDlgItem(hwnd, IDC_REPLACETEXT))) {
if (StrgIsNotEmpty(s_pEfrData->chReplaceTemplate)) {
ComboBox_SetTextHW(hwnd, IDC_REPLACETEXT, s_pEfrData->chReplaceTemplate);
Expand All @@ -6996,6 +7033,9 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
SetDlgPos(hwnd, s_xFindReplaceDlgSave, s_yFindReplaceDlgSave);
s_bSwitchedFindReplace = false;
}
else if (pInit->pRestore) {
_RestoreFindReplaceDlgPosition(hwnd, &pInit->pRestore->windowRect);
}
else {
if (Settings.FindReplaceDlgPosX == CW_USEDEFAULT || Settings.FindReplaceDlgPosY == CW_USEDEFAULT) {
CenterDlgInParent(hwnd, false);
Expand Down Expand Up @@ -7050,6 +7090,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);
Expand Down Expand Up @@ -7374,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 {
Expand Down Expand Up @@ -7422,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;
Expand Down Expand Up @@ -7799,17 +7852,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 FindReplaceDlgRestore* pRestore)
{
lpefr->hwnd = hwnd;
FindReplaceDlgInit const init = { lpefr, pRestore };
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 (pRestore) {
_EnableFRDlgCtrls(hDlg);
}
UpdateStatusbar(true);
}
return hDlg;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
Expand Down
40 changes: 18 additions & 22 deletions src/MuiLanguage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}


Expand Down Expand Up @@ -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);
DestroyMenu(Globals.hMainMenu);

// desired language
LPCWSTR desiredLocaleName = MUI_LanguageDLLs[iLngIdx].LocaleName;
SetMuiLanguage(LoadLanguageResources(desiredLocaleName));
LoadLanguageResources(MUI_LanguageDLLs[iLngIdx].LocaleName);

Globals.hMainMenu = LoadMenu(Globals.hLngResContainer, MAKEINTRESOURCE(IDR_MUI_MAINMENU));
if (!Globals.hMainMenu) {
InfoBoxLastError(L"LoadMenu()", 0);
CloseApplication();
return;
}

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;
}


Expand Down
4 changes: 2 additions & 2 deletions src/MuiLanguage.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ 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);
bool DynamicLanguageMenuCmd(int cmd);

#endif // HAVE_DYN_LOAD_LIBS_MUI_LNGS

Expand Down
Loading