From 2b99b48fc9c4c91b567a73ef0dbad3badd1a96f3 Mon Sep 17 00:00:00 2001 From: "METANEOCORTEX\\Kotti" Date: Mon, 27 May 2024 18:27:57 +0200 Subject: [PATCH] +fix: finding np3 instance holding same file-path (only one instance) --- src/Notepad3.c | 70 +++++++++++++++++++++++++------------------------- src/Notepad3.h | 2 +- src/PathLib.c | 4 +-- src/PathLib.h | 1 - 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/Notepad3.c b/src/Notepad3.c index e923c8a43..3c7f24fae 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -1321,7 +1321,7 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, return FALSE; } // Try to activate another window - if (ActivatePrevInst()) { + if (ActivatePrevInst(!bIsAutoLoadMostRecent)) { if (!bIsAutoLoadMostRecent) { return FALSE; } @@ -1626,14 +1626,6 @@ static BOOL CALLBACK _EnumWndProc2(HWND hwnd, LPARAM lParam) if (StrCmpW(szClassName, s_wchWndClass) == 0) { - UINT const iReuseLock = GetDlgItemInt(hwnd, IDC_REUSELOCK, NULL, FALSE); - if ((GetTicks_ms() - iReuseLock) >= REUSEWINDOWLOCKTIMEOUT) { - - if (IsWindowEnabled(hwnd)) { - bContinue = FALSE; - } - } - WCHAR wchFileName[INTERNET_MAX_URL_LENGTH] = { L'\0' }; GetDlgItemText(hwnd, IDC_FILENAME, wchFileName, COUNTOF(wchFileName)); @@ -1649,6 +1641,15 @@ static BOOL CALLBACK _EnumWndProc2(HWND hwnd, LPARAM lParam) } +static bool FindOtherInstance(HWND* phwnd, HPATHL hpthFileName) +{ + Path_Reset(s_pthCheckFilePath, Path_Get(hpthFileName)); + *phwnd = NULL; + EnumWindows(_EnumWndProc2, (LPARAM)phwnd); + return (*phwnd != NULL); +} + + //============================================================================= // // _StatusCalcTextSize() @@ -2782,6 +2783,13 @@ static void _InitEditWndFrame() } +static void _SetEnumWindowsItems(HWND hwnd) +{ + SetDlgItemText(hwnd, IDC_FILENAME, Path_Get(Paths.CurrentFile)); + SetDlgItemInt(hwnd, IDC_REUSELOCK, (UINT)GetTicks_ms(), false); +} + + //============================================================================= // // MsgCreate() - Handles WM_CREATE @@ -2845,8 +2853,6 @@ LRESULT MsgCreate(HWND hwnd, WPARAM wParam,LPARAM lParam) hInstance, NULL); - SetDlgItemText(hwnd,IDC_FILENAME,Path_Get(Paths.CurrentFile)); - (void)CreateWindow( WC_STATIC, NULL, @@ -2857,7 +2863,7 @@ LRESULT MsgCreate(HWND hwnd, WPARAM wParam,LPARAM lParam) hInstance, NULL); - SetDlgItemInt(hwnd,IDC_REUSELOCK,(UINT)GetTicks_ms(),false); + _SetEnumWindowsItems(hwnd); // Menu //~SetMenuDefaultItem(GetSubMenu(GetMenu(hwnd),0),0); @@ -4709,6 +4715,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) } switch(iLoWParam) { + case SCEN_CHANGE: EditUpdateVisibleIndicators(); MarkAllOccurrences(-1, false); @@ -4738,7 +4745,6 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) _SplitUndoTransaction(); break; - case IDM_FILE_NEW: { HPATHL hfile_pth = Path_Allocate(L""); FileLoadFlags fLoadFlags = FLF_New; @@ -11488,8 +11494,7 @@ bool FileLoad(const HPATHL hfile_pth, const FileLoadFlags fLoadFlags, const DocP } Path_Empty(Paths.CurrentFile, false); - SetDlgItemText(Globals.hwndMain, IDC_FILENAME, Path_Get(Paths.CurrentFile)); - SetDlgItemInt(Globals.hwndMain, IDC_REUSELOCK, (UINT)GetTicks_ms(), false); + _SetEnumWindowsItems(Globals.hwndMain); if (!s_flagKeepTitleExcerpt) { StringCchCopy(s_wchTitleExcerpt, COUNTOF(s_wchTitleExcerpt), L""); } @@ -11541,15 +11546,14 @@ bool FileLoad(const HPATHL hfile_pth, const FileLoadFlags fLoadFlags, const DocP Path_NormalizeEx(hopen_file, Paths.WorkingDirectory, true, Flags.bSearchPathIfRelative); - if (!bReloadFile && Path_StrgComparePathNormalized(hopen_file, Paths.CurrentFile) == 0) { + if (!bReloadFile && Path_StrgComparePath(hopen_file, Paths.CurrentFile, Paths.WorkingDirectory) == 0) { Path_Release(hopen_file); return false; } if (!bReloadFile && Flags.bSingleFileInstance) { - Path_Reset(s_pthCheckFilePath, Path_Get(hopen_file)); + HWND hwnd = NULL; - EnumWindows(_EnumWndProc2, (LPARAM)&hwnd); - if (hwnd != NULL) { + if (FindOtherInstance(&hwnd, hopen_file)) { if (!s_bInitAppDone || IsYesOkay(InfoBoxLng(MB_YESNO | MB_ICONQUESTION, L"InfoInstanceExist", IDS_MUI_ASK_INSTANCE_EXISTS))) { if (IsIconic(hwnd)) { ShowWindowAsync(hwnd, SW_RESTORE); @@ -11558,6 +11562,7 @@ bool FileLoad(const HPATHL hfile_pth, const FileLoadFlags fLoadFlags, const DocP SendMessage(hwnd, WM_TRAYMESSAGE, 0, WM_LBUTTONDBLCLK); SendMessage(hwnd, WM_TRAYMESSAGE, 0, WM_LBUTTONUP); } + LockSetForegroundWindow(LSFW_UNLOCK); SetForegroundWindow(hwnd); } Path_Release(hopen_file); @@ -11640,8 +11645,7 @@ bool FileLoad(const HPATHL hfile_pth, const FileLoadFlags fLoadFlags, const DocP //~Path_Swap(Paths.CurrentFile, hopen_file); ~ hopen_file needed later Path_Reset(Paths.CurrentFile, Path_Get(hopen_file)); // dup - SetDlgItemText(Globals.hwndMain, IDC_FILENAME, Path_Get(Paths.CurrentFile)); - SetDlgItemInt(Globals.hwndMain, IDC_REUSELOCK, (UINT)GetTicks_ms(), false); + _SetEnumWindowsItems(Globals.hwndMain); if (!s_flagKeepTitleExcerpt) { StringCchCopy(s_wchTitleExcerpt, COUNTOF(s_wchTitleExcerpt), L""); @@ -11680,7 +11684,7 @@ bool FileLoad(const HPATHL hfile_pth, const FileLoadFlags fLoadFlags, const DocP } // consistent settings file handling (if loaded in editor) - Flags.bSettingsFileSoftLocked = (Path_StrgComparePathNormalized(Paths.CurrentFile, Paths.IniFile) == 0); + Flags.bSettingsFileSoftLocked = (Path_StrgComparePath(Paths.CurrentFile, Paths.IniFile, Paths.WorkingDirectory) == 0); // the .LOG feature ... if (IsFileVarLogFile()) { @@ -12087,8 +12091,7 @@ bool FileSave(FileSaveFlags fSaveFlags) if (fSuccess) { if (!(fSaveFlags & FSF_SaveCopy)) { Path_Swap(Paths.CurrentFile, hfile_pth); - SetDlgItemText(Globals.hwndMain, IDC_FILENAME, Path_Get(Paths.CurrentFile)); - SetDlgItemInt(Globals.hwndMain, IDC_REUSELOCK, (UINT)GetTicks_ms(), false); + _SetEnumWindowsItems(Globals.hwndMain); if (!s_flagKeepTitleExcerpt) { StringCchCopy(s_wchTitleExcerpt, COUNTOF(s_wchTitleExcerpt), L""); } @@ -12212,7 +12215,7 @@ int CountRunningInstances() { * ******************************************************************************/ -bool ActivatePrevInst() +bool ActivatePrevInst(const bool bSetForground) { HWND hwnd = NULL; COPYDATASTRUCT cds = { 0 }; @@ -12225,10 +12228,8 @@ bool ActivatePrevInst() Path_NormalizeEx(s_pthArgFilePath, Paths.WorkingDirectory, true, Flags.bSearchPathIfRelative); - Path_Reset(s_pthCheckFilePath, Path_Get(s_pthArgFilePath)); + if (FindOtherInstance(&hwnd, s_pthArgFilePath)) { - EnumWindows(_EnumWndProc2,(LPARAM)&hwnd); - if (hwnd != NULL) { // Enabled if (IsWindowEnabled(hwnd)) { @@ -12242,8 +12243,9 @@ bool ActivatePrevInst() SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONDBLCLK); SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONUP); } - SetForegroundWindow(hwnd); - + if (bSetForground) { + SetForegroundWindow(hwnd); + } size_t cb = sizeof(np3params); if (s_lpSchemeArg) { cb += ((StringCchLen(s_lpSchemeArg, 0) + 1) * sizeof(WCHAR)); @@ -12298,11 +12300,9 @@ bool ActivatePrevInst() return false; } - hwnd = NULL; - EnumWindows(_EnumWndProc,(LPARAM)&hwnd); - // Found a window - if (hwnd != NULL) { + hwnd = NULL; + if (EnumWindows(_EnumWndProc, (LPARAM)&hwnd)) { // Enabled if (IsWindowEnabled(hwnd)) { @@ -12316,7 +12316,7 @@ bool ActivatePrevInst() SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONDBLCLK); SendMessage(hwnd,WM_TRAYMESSAGE,0,WM_LBUTTONUP); } - + // always set foreground (ignoring: bSetForground here) SetForegroundWindow(hwnd); if (Path_IsNotEmpty(s_pthArgFilePath)) diff --git a/src/Notepad3.h b/src/Notepad3.h index 09e88504a..76320d377 100644 --- a/src/Notepad3.h +++ b/src/Notepad3.h @@ -112,7 +112,7 @@ void CreateBars(HWND hwnd, HINSTANCE hInstance); WININFO GetFactoryDefaultWndPos(HWND hwnd, const int flagsPos); WININFO GetWinInfoByFlag(HWND hwnd, const int flagsPos); int CountRunningInstances(); -bool ActivatePrevInst(); +bool ActivatePrevInst(const bool bSetForground); bool LaunchNewInstance(HWND hwnd, LPCWSTR lpszParameter, LPCWSTR lpszFilePath); bool RelaunchMultiInst(); bool RelaunchElevated(LPCWSTR lpNewCmdLnArgs); diff --git a/src/PathLib.c b/src/PathLib.c index 2f9001bf6..3ff3208a8 100644 --- a/src/PathLib.c +++ b/src/PathLib.c @@ -964,7 +964,7 @@ bool PTHAPI Path_IsExistingDirectory(const HPATHL hpth) // ---------------------------------------------------------------------------- -int PTHAPI Path_StrgComparePathNormalized(const HPATHL hpth1, const HPATHL hpth2) +static int _StrgComparePathNormalized(const HPATHL hpth1, const HPATHL hpth2) { if (Path_IsEmpty(hpth1)) { return -1; } if (Path_IsEmpty(hpth2)) { return +1; } @@ -987,7 +987,7 @@ int PTHAPI Path_StrgComparePath(const HPATHL hpth1, const HPATHL hpth2, const HP Path_NormalizeEx(hpth1_tmp, hpth_wrkdir, true, false); Path_NormalizeEx(hpth2_tmp, hpth_wrkdir, true, false); - int const cmp = Path_StrgComparePathNormalized(hpth1_tmp, hpth2_tmp); + int const cmp = _StrgComparePathNormalized(hpth1_tmp, hpth2_tmp); Path_Release(hpth1_tmp); Path_Release(hpth2_tmp); diff --git a/src/PathLib.h b/src/PathLib.h index f45eafa58..8a026a484 100644 --- a/src/PathLib.h +++ b/src/PathLib.h @@ -99,7 +99,6 @@ bool PTHAPI Path_IsRoot(const HPATHL hpth); bool PTHAPI Path_IsValidUNC(const HPATHL hpth); bool PTHAPI Path_IsExistingDirectory(const HPATHL hpth); -int PTHAPI Path_StrgComparePathNormalized(const HPATHL hpth1, const HPATHL hpth2); int PTHAPI Path_StrgComparePath(const HPATHL hpth1, const HPATHL hpth2, const HPATHL hpth_wrkdir); bool PTHAPI Path_RemoveFileSpec(HPATHL hpth_in_out); bool PTHAPI Path_RenameExtension(HPATHL hpth, LPCWSTR ext);