Skip to content

Commit

Permalink
Merge pull request rizonesoft#5196 from RaiKoHoff/Dev_Master
Browse files Browse the repository at this point in the history
Fix: finding np3 instance holding same file-path (only one instance)
  • Loading branch information
hpwamr committed May 27, 2024
2 parents a0cdc64 + 2b99b48 commit a4f9516
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
70 changes: 35 additions & 35 deletions src/Notepad3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -4709,6 +4715,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
}

switch(iLoWParam) {

case SCEN_CHANGE:
EditUpdateVisibleIndicators();
MarkAllOccurrences(-1, false);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"");
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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"");
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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"");
}
Expand Down Expand Up @@ -12212,7 +12215,7 @@ int CountRunningInstances() {
*
******************************************************************************/

bool ActivatePrevInst()
bool ActivatePrevInst(const bool bSetForground)
{
HWND hwnd = NULL;
COPYDATASTRUCT cds = { 0 };
Expand All @@ -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)) {

Expand All @@ -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));
Expand Down Expand Up @@ -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)) {

Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/Notepad3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/PathLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/PathLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a4f9516

Please sign in to comment.