Skip to content

Commit

Permalink
add SimpleBrowserWindow.[cpp|h]
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 29, 2024
1 parent 387a226 commit 603854b
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 66 deletions.
1 change: 1 addition & 0 deletions premake5.files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ function sumatrapdf_files()
"Selection.*",
"Settings.h",
"SettingsStructs.*",
"SimpleBrowserWindow.*",
"SumatraPDF.cpp",
"SumatraPDF.h",
"SumatraPDF.rc",
Expand Down
74 changes: 11 additions & 63 deletions src/Scratch.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* Copyright 2022 the SumatraPDF project authors (see AUTHORS file).
/* Copyright 2024 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */

// this is for adding temporary code for testing

// TODO: remove this
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
// #define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS

#include "utils/BaseUtil.h"
#include "utils/Archive.h"
Expand All @@ -22,6 +22,7 @@

#include "Settings.h"
#include "DocProperties.h"
#include "SimpleBrowserWindow.h"
#include "DocController.h"
#include "PalmDbReader.h"
#include "EbookBase.h"
Expand All @@ -34,66 +35,13 @@

// ----------------

struct BrowserTestWnd : Wnd {
Webview2Wnd* webView = nullptr;
LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override;
~BrowserTestWnd() {
delete webView;
}
};

LRESULT BrowserTestWnd::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
if (msg == WM_CLOSE) {
OnClose();
return 0;
}
if (msg == WM_DESTROY) {
PostQuitMessage(0);
return 0;
}
if (msg == WM_SIZE && webView) {
Rect rc = ClientRect(hwnd);
rc.x += 10;
rc.y += 10;
rc.dx -= 20;
rc.dy -= 20;
webView->SetBounds(rc);
}
return WndProcDefault(hwnd, msg, wparam, lparam);
}

void TestBrowser() {
int dx = 480;
int dy = 640;
auto w = new BrowserTestWnd();
{
CreateCustomArgs args;
args.pos = {CW_USEDEFAULT, CW_USEDEFAULT, dx, dy};
args.title = "test browser";
// TODO: if set, navigate to url doesn't work
// args.visible = false;
HWND hwnd = w->CreateCustom(args);
ReportIf(!hwnd);
}

{
Rect rc = ClientRect(w->hwnd);
w->webView = new Webview2Wnd();
w->webView->dataDir = str::Dup(AppGenDataFilenameTemp("webViewData"));
CreateWebViewArgs args;
args.parent = w->hwnd;
dx = rc.dx;
dy = rc.dy;
args.pos = {10, 10, dx - 20, dy - 20};
HWND hwnd = w->webView->Create(args);
ReportIf(!hwnd);
w->webView->SetIsVisible(true);
}

// important to call this after hooking up onSize to ensure
// first layout is triggered
w->webView->Navigate("https://blog.kowalczyk.info/");
w->SetIsVisible(true);
RunMessageLoop(nullptr, w->hwnd);
delete w;
SimpleBrowserCreateArgs args;
args.title = "Test Browser Window";
args.url = "https://blog.kowalczyk.info/";
args.pos = {CW_USEDEFAULT, CW_USEDEFAULT, 480, 640};
auto w = new SimpleBrowserWindow();
w->Create(args);
// RunMessageLoop(nullptr, w->hwnd);
// delete w;
}
82 changes: 82 additions & 0 deletions src/SimpleBrowserWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* Copyright 2024 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */

#include "utils/BaseUtil.h"
#include "utils/Archive.h"
#include "utils/FileUtil.h"
#include "utils/WinUtil.h"
#include "utils/GdiPlusUtil.h"
#include "utils/GuessFileType.h"
#include "utils/Timer.h"
#include "utils/ZipUtil.h"

#include "wingui/UIModels.h"
#include "wingui/Layout.h"
#include "wingui/WinGui.h"
#include "wingui/WebView.h"

#include "AppTools.h"

#include "SimpleBrowserWindow.h"

SimpleBrowserWindow::~SimpleBrowserWindow() {
delete webView;
}

LRESULT SimpleBrowserWindow::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
if (msg == WM_CLOSE) {
OnClose();
return 0;
}
if (msg == WM_SIZE && webView) {
Rect rc = ClientRect(hwnd);
rc.x += 10;
rc.y += 10;
rc.dx -= 20;
rc.dy -= 20;
webView->SetBounds(rc);
}
return WndProcDefault(hwnd, msg, wparam, lparam);
}

HWND SimpleBrowserWindow::Create(const SimpleBrowserCreateArgs& args) {
HWND hwnd = nullptr;
{
CreateCustomArgs cargs;
cargs.pos = args.pos;
if (cargs.pos.IsZero()) {
cargs.pos = {CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT};
}
cargs.title = args.title;
if (!cargs.title) {
cargs.title = "Browser Window";
}
// TODO: if set, navigate to url doesn't work
// args.visible = false;
hwnd = CreateCustom(cargs);
ReportIf(!hwnd);
}
{
Rect rc = ClientRect(hwnd);
webView = new Webview2Wnd();
const char* dataDir = args.dataDir;
if (!dataDir) {
dataDir = AppGenDataFilenameTemp("webViewData");
}
webView->dataDir = str::Dup(dataDir);
CreateWebViewArgs cargs;
cargs.parent = hwnd;
cargs.pos = {10, 10, rc.dx - 20, rc.dy - 20};
hwnd = webView->Create(cargs);
if (!hwnd) {
// ReportIfQuick(!hwnd);
return nullptr;
}
webView->SetIsVisible(true);
}
// important to call this after hooking up onSize to ensure
// first layout is triggered
webView->Navigate(args.url);
SetIsVisible(true);
return hwnd;
}
16 changes: 16 additions & 0 deletions src/SimpleBrowserWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright 2024 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */

struct SimpleBrowserCreateArgs {
const char* title = nullptr;
Rect pos{}; // if empty, will use CW_USEDEFAULT
const char* url = nullptr;
const char* dataDir = nullptr;
};

struct SimpleBrowserWindow : Wnd {
Webview2Wnd* webView = nullptr;
HWND Create(const SimpleBrowserCreateArgs&);
LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override;
~SimpleBrowserWindow() override;
};
6 changes: 5 additions & 1 deletion src/utils/GeomUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2022 the SumatraPDF project authors (see AUTHORS file).
/* Copyright 2024 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */

#include "BaseUtil.h"
Expand Down Expand Up @@ -127,6 +127,10 @@ Rect Rect::FromXY(Point TL, Point BR) {
return FromXY(TL.x, TL.y, BR.x, BR.y);
}

bool Rect::IsZero() const {
return x == 0 && y == 0 && dx == 0 && dy == 0;
}

bool Rect::IsEmpty() const {
return dx == 0 || dy == 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/GeomUtil.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2022 the SumatraPDF project authors (see AUTHORS file).
/* Copyright 2024 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */

// note: include BaseUtil.h instead of including directly
Expand Down Expand Up @@ -76,6 +76,7 @@ struct Rect {
int Bottom() const;
static Rect FromXY(int xs, int ys, int xe, int ye);
static Rect FromXY(const Point TL, const Point BR);
bool IsZero() const;
bool IsEmpty() const;
bool Contains(int x, int y) const;
bool Contains(const Point pt) const;
Expand Down
2 changes: 1 addition & 1 deletion src/wingui/HtmlWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2022 the SumatraPDF project authors (see AUTHORS file).
/* Copyright 2024 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */

#include "utils/BaseUtil.h"
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions vs2022/SumatraPDF-dll.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@
<ClInclude Include="..\src\SearchAndDDE.h" />
<ClInclude Include="..\src\Selection.h" />
<ClInclude Include="..\src\Settings.h" />
<ClInclude Include="..\src\SimpleBrowserWindow.h" />
<ClInclude Include="..\src\StressTesting.h" />
<ClInclude Include="..\src\SumatraDialogs.h" />
<ClInclude Include="..\src\SumatraPDF.h" />
Expand Down Expand Up @@ -1114,6 +1115,7 @@
</ClCompile>
<ClCompile Include="..\src\SearchAndDDE.cpp" />
<ClCompile Include="..\src\Selection.cpp" />
<ClCompile Include="..\src\SimpleBrowserWindow.cpp" />
<ClCompile Include="..\src\StressTesting.cpp" />
<ClCompile Include="..\src\SumatraConfig.cpp" />
<ClCompile Include="..\src\SumatraDialogs.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions vs2022/SumatraPDF-dll.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
<ClInclude Include="..\src\Settings.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\src\SimpleBrowserWindow.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\src\StressTesting.h">
<Filter>src</Filter>
</ClInclude>
Expand Down Expand Up @@ -389,6 +392,9 @@
<ClCompile Include="..\src\Selection.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\SimpleBrowserWindow.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\StressTesting.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions vs2022/SumatraPDF.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,7 @@
<ClInclude Include="..\src\SearchAndDDE.h" />
<ClInclude Include="..\src\Selection.h" />
<ClInclude Include="..\src\Settings.h" />
<ClInclude Include="..\src\SimpleBrowserWindow.h" />
<ClInclude Include="..\src\StressTesting.h" />
<ClInclude Include="..\src\SumatraDialogs.h" />
<ClInclude Include="..\src\SumatraPDF.h" />
Expand Down Expand Up @@ -1065,6 +1066,7 @@
</ClCompile>
<ClCompile Include="..\src\SearchAndDDE.cpp" />
<ClCompile Include="..\src\Selection.cpp" />
<ClCompile Include="..\src\SimpleBrowserWindow.cpp" />
<ClCompile Include="..\src\StressTesting.cpp" />
<ClCompile Include="..\src\SumatraConfig.cpp" />
<ClCompile Include="..\src\SumatraDialogs.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions vs2022/SumatraPDF.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
<ClInclude Include="..\src\Settings.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\src\SimpleBrowserWindow.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\src\StressTesting.h">
<Filter>src</Filter>
</ClInclude>
Expand Down Expand Up @@ -386,6 +389,9 @@
<ClCompile Include="..\src\Selection.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\SimpleBrowserWindow.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\StressTesting.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down

0 comments on commit 603854b

Please sign in to comment.