Skip to content

Commit

Permalink
target the right file when processing a sequence of DDE commands (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Kowalczyk committed Nov 24, 2023
1 parent e5faa87 commit 95ecfa7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/SearchAndDDE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,8 @@ static const char* HandleCmdCommand(HWND hwnd, const char* cmd, bool* ack) {

// returns true if did handle a message
static bool HandleExecuteCmds(HWND hwnd, const char* cmd) {
gMostRecentlyOpenedDoc = nullptr;

bool didHandle = false;
while (!str::IsEmpty(cmd)) {
{
Expand Down
31 changes: 29 additions & 2 deletions src/SumatraPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ void SwitchToDisplayMode(MainWindow* win, DisplayMode displayMode, bool keepCont
UpdateToolbarState(win);
}

static WindowTab* FindTabByController(DocController* ctrl) {
for (MainWindow* win : gWindows) {
for (WindowTab* tab : win->Tabs()) {
if (tab->ctrl == ctrl) {
return tab;
}
}
}
return nullptr;
}

WindowTab* FindTabByFile(const char* file) {
char* normFile = path::NormalizeTemp(file);

Expand Down Expand Up @@ -373,7 +384,13 @@ void SelectTabInWindow(WindowTab* tab) {

// Find the first window showing a given PDF file
MainWindow* FindMainWindowByFile(const char* file, bool focusTab) {
WindowTab* tab = FindTabByFile(file);
WindowTab* tab = nullptr;
if (gMostRecentlyOpenedDoc && path::IsSame(gMostRecentlyOpenedDoc->GetFilePath(), file)) {
tab = FindTabByController(gMostRecentlyOpenedDoc);
}
if (!tab) {
tab = FindTabByFile(file);
}
if (!tab) {
return nullptr;
}
Expand Down Expand Up @@ -934,6 +951,13 @@ static DocController* CreateControllerForChm(const char* path, PasswordUI* pwdUI
return ctrl;
}

// this allows us to target the right file when processing
// a sequence of DDE commands. Without this commands target
// the tab by path and if there's more than one with the same
// path, we pick the first one
// https://github.com/sumatrapdfreader/sumatrapdf/issues/3903
DocController* gMostRecentlyOpenedDoc = nullptr;

DocController* CreateControllerForEngineOrFile(EngineBase* engine, const char* path, PasswordUI* pwdUI,
MainWindow* win) {
// TODO: move this to MainWindow constructor
Expand All @@ -948,7 +972,9 @@ DocController* CreateControllerForEngineOrFile(EngineBase* engine, const char* p
}
if (!engine) {
// as a last resort, try to open as chm file
return CreateControllerForChm(path, pwdUI, win);
auto ctrl = CreateControllerForChm(path, pwdUI, win);
gMostRecentlyOpenedDoc = ctrl;
return ctrl;
}
int nPages = engine ? engine->pageCount : 0;
logf("CreateControllerForEngineOrFile: '%s', %d pages\n", path, nPages);
Expand All @@ -960,6 +986,7 @@ DocController* CreateControllerForEngineOrFile(EngineBase* engine, const char* p
DocController* ctrl = new DisplayModel(engine, win->cbHandler);
CrashIf(!ctrl || !ctrl->AsFixed() || ctrl->AsChm());
VerifyController(ctrl, path);
gMostRecentlyOpenedDoc = ctrl;
return ctrl;
}

Expand Down
3 changes: 3 additions & 0 deletions src/SumatraPDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ extern HCURSOR gCursorDrag;
extern bool gCrashOnOpen;
extern HWND gLastActiveFrameHwnd;

struct DocController;
extern DocController* gMostRecentlyOpenedDoc;

#define gPluginMode (gPluginURL != nullptr)

void InitializePolicies(bool restrict);
Expand Down

0 comments on commit 95ecfa7

Please sign in to comment.