Skip to content

Commit

Permalink
Add Generate Report menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Jul 16, 2023
1 parent 4a7715c commit ba22942
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/WinWebDiff/Resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDM_FILE_NEW 106
#define IDM_FILE_NEW_TAB 107
#define IDM_FILE_CLOSE_TAB 108
#define IDM_FILE_RELOAD 109
#define IDM_FILE_NEW3 107
#define IDM_FILE_NEW_TAB 108
#define IDM_FILE_CLOSE_TAB 109
#define IDM_FILE_RELOAD 110
#define IDM_FILE_GENERATE_REPORT 111
#define IDM_VIEW_SIZE_FIT_TO_WINDOW 120
#define IDM_VIEW_SIZE_320x512 121
#define IDM_VIEW_SIZE_375x600 122
Expand Down
125 changes: 125 additions & 0 deletions src/WinWebDiff/WinWebDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
#include <combaseapi.h>
#include <shellapi.h>
#include <CommCtrl.h>
#include <commdlg.h>
#include <Shlwapi.h>
#include <string>
#include <vector>
#include <list>
#include <filesystem>
#include <iostream>
#include <fstream>
#include <wrl.h>

#pragma comment(lib, "comctl32.lib")
Expand Down Expand Up @@ -265,6 +269,97 @@ bool CompareFiles(const std::vector<std::wstring>& filenames, const std::wstring
return true;
}

std::string ToUTF8(const wchar_t* text)
{
int size = WideCharToMultiByte(CP_UTF8, 0, text, -1, nullptr, 0, NULL, NULL);
std::string utf8(size, 0);
WideCharToMultiByte(CP_UTF8, 0, text, -1, utf8.data(), static_cast<int>(utf8.size()), NULL, NULL);
utf8.resize(strlen(utf8.c_str()));
return utf8;
}

void AppMsgBox(const std::wstring& text, int types)
{
PostMessage(m_hWnd, WM_USER + 100, reinterpret_cast<WPARAM>(new std::wstring(text)), types);
}

bool GenerateHTMLReport(const wchar_t* filename)
{
const int BUFFER_SIZE = 4096;
wchar_t tmp[BUFFER_SIZE];
wchar_t rptdir_full[BUFFER_SIZE];
std::wstring rptdir;
std::wstring rptfilepath[3];
std::wstring difffilename[3];
std::vector<std::string> rptfilepath_utf8, difffilename_utf8;
wcscpy_s(rptdir_full, filename);
PathRemoveExtensionW(rptdir_full);
PathAddExtensionW(rptdir_full, L".files");
rptdir = PathFindFileName(rptdir_full);
CreateDirectoryW(rptdir_full, nullptr);
const wchar_t* pfilenames[3]{};
std::vector<std::wstring> filenames;
for (int i = 0; i < m_pWebDiffWindow->GetPaneCount(); ++i)
{
rptfilepath[i] = m_pWebDiffWindow->GetCurrentUrl(i);
rptfilepath_utf8.push_back(ToUTF8(rptfilepath[i].c_str()));
wsprintfW(tmp, L"%d.pdf", i + 1);
difffilename[i] = rptdir + L"/" + tmp;
difffilename_utf8.push_back(ToUTF8(difffilename[i].c_str()));
filenames.push_back(std::wstring(rptdir_full) + L"/" + tmp);
pfilenames[i] = filenames[i].c_str();
}
std::ofstream fout;
try
{
fout.open(filename, std::ios::out | std::ios::trunc);
fout <<
"<!DOCTYPE html>" << std::endl <<
"<html>" << std::endl <<
"<head>" << std::endl <<
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" << std::endl <<
"<title>WinMerge Webpage Compare Report</title>" << std::endl <<
"<style type=\"text/css\">" << std::endl <<
"table { table-layout: fixed; width: 100%; border-collapse: collapse; }" << std::endl <<
"th,td { border: solid 1px black; }" << std::endl <<
"embed { width: 100%; height: calc(100vh - 56px) }" << std::endl <<
".title { color: white; background-color: blue; vertical-align: top; }" << std::endl <<
"</style>" << std::endl <<
"</head>" << std::endl <<
"<body>" << std::endl <<
"<table>" << std::endl <<
"<tr>" << std::endl;
for (int i = 0; i < m_pWebDiffWindow->GetPaneCount(); ++i)
fout << "<th class=\"title\">" << rptfilepath_utf8[i] << "</th>" << std::endl;
fout <<
"</tr>" << std::endl <<
"<tr>" << std::endl;
for (int i = 0; i < m_pWebDiffWindow->GetPaneCount(); ++i)
fout << "<td><embed type=\"application/pdf\" src=\"" << difffilename_utf8[i] <<
"\" title=\"" << difffilename_utf8[i] << "\"></td>" << std::endl;
fout <<
"</tr>" << std::endl <<
"</table>" << std::endl <<
"</body>" << std::endl <<
"</html>" << std::endl;
}
catch (...)
{
return false;
}
m_pWebDiffWindow->SaveDiffFiles(IWebDiffWindow::PDF, pfilenames,
Callback<IWebDiffCallback>([](const WebDiffCallbackResult& result) -> HRESULT
{
if (SUCCEEDED(result.errorCode))
AppMsgBox(L"The report has been created successfully.", MB_OK | MB_ICONINFORMATION);
else
AppMsgBox(L"Failed to create the report", MB_OK | MB_ICONWARNING);
return S_OK;
})
.Get());
return true;
}

void UpdateMenuState(HWND hWnd)
{
HMENU hMenu = GetMenu(hWnd);
Expand Down Expand Up @@ -300,6 +395,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case IDM_FILE_NEW:
m_pWebDiffWindow->New(2, nullptr);
break;
case IDM_FILE_NEW3:
m_pWebDiffWindow->New(3, nullptr);
break;
case IDM_FILE_NEW_TAB:
{
int nActivePane = m_pWebDiffWindow->GetActivePane();
Expand All @@ -315,6 +413,26 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case IDM_FILE_RELOAD:
m_pWebDiffWindow->ReloadAll();
break;
case IDM_FILE_GENERATE_REPORT:
{
wchar_t szFileName[MAX_PATH] = {0}, szFile[MAX_PATH] = {0};
OPENFILENAMEW ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = L"HTML file(*.html)\0*.html\0\0";
ofn.lpstrFile = szFileName;
ofn.lpstrFileTitle = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.nMaxFileTitle = sizeof(szFile);
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
ofn.lpstrTitle = L"Generate HTML Report File";
ofn.lpstrDefExt = L"html";
if (GetSaveFileNameW(&ofn) != 0)
{
GenerateHTMLReport(ofn.lpstrFile);
}
break;
}
case IDM_EXIT:
DestroyWindow(hWnd);
break;
Expand Down Expand Up @@ -482,6 +600,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
}
break;
}
case WM_USER + 100:
{
std::wstring* ptext = reinterpret_cast<std::wstring*>(wParam);
MessageBoxW(m_hWnd, ptext->c_str(), L"WinWebDiff", static_cast<int>(lParam));
delete ptext;
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
Expand Down
Binary file modified src/WinWebDiff/WinWebDiff.rc
Binary file not shown.
16 changes: 16 additions & 0 deletions src/WinWebDiffLib/WebDiffWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,22 @@ class CWebDiffWindow : public IWebDiffWindow
}).Get());
}

HRESULT SaveDiffFiles(FormatType kind, const wchar_t* filenames[], IWebDiffCallback* callback) override
{
auto sfilenames = std::make_shared<std::vector<std::wstring>>();
for (int pane = 0; pane < m_nPanes; ++pane)
sfilenames->push_back(filenames[pane]);
ComPtr<IWebDiffCallback> callback2(callback);
HRESULT hr = saveFilesLoop(kind, sfilenames,
Callback<IWebDiffCallback>([this, sfilenames, callback2](const WebDiffCallbackResult& result) -> HRESULT
{
if (callback2)
return callback2->Invoke({ result.errorCode, nullptr });
return S_OK;
}).Get());
return hr;
}

HRESULT ClearBrowsingData(int pane, BrowsingDataType datakinds) override
{
int spane = pane, epane = pane;
Expand Down
29 changes: 29 additions & 0 deletions src/WinWebDiffLib/WebWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ class CWebWindow
return SaveText(filename, callback);
case IWebDiffWindow::RESOURCETREE:
return SaveResourceTree(filename, callback);
case IWebDiffWindow::PDF:
return SavePDF(filename, callback);
default:
return E_INVALIDARG;
}
Expand Down Expand Up @@ -974,6 +976,33 @@ class CWebWindow
return hr;
}

HRESULT SavePDF(const std::wstring& filename, IWebDiffCallback* callback)
{
if (!GetActiveWebView())
return E_FAIL;
ComPtr<IWebDiffCallback> callback2(callback);
wil::com_ptr<ICoreWebView2_7> webview2_7 = GetActiveTab()->m_webview.try_query<ICoreWebView2_7>();
if (!webview2_7)
return E_NOINTERFACE;
wil::com_ptr<ICoreWebView2PrintSettings> printSettings;
wil::com_ptr<ICoreWebView2Environment6> webviewEnvironment6 = m_webviewEnvironment.try_query<ICoreWebView2Environment6>();
if (webviewEnvironment6)
{
webviewEnvironment6->CreatePrintSettings(&printSettings);
printSettings->put_ShouldPrintBackgrounds(true);
}
HRESULT hr = webview2_7->PrintToPdf(filename.c_str(), printSettings.get(),
Callback<ICoreWebView2PrintToPdfCompletedHandler>(
[callback2](HRESULT errorCode, BOOL isSucessful) -> HRESULT {
if (callback2)
return callback2->Invoke({ errorCode, nullptr});
return S_OK;
}).Get());
if (FAILED(hr) && callback2)
return callback2->Invoke({ hr, nullptr });
return hr;
}

HRESULT CallDevToolsProtocolMethod(const wchar_t* methodName, const wchar_t* params, IWebDiffCallback *callback, bool showError = true)
{
if (!GetActiveWebView())
Expand Down
3 changes: 2 additions & 1 deletion src/WinWebDiffLib/WinWebDiffLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct IWebDiffWindow
};
enum FormatType
{
SCREENSHOT, FULLSIZE_SCREENSHOT, HTML, TEXT, RESOURCETREE
SCREENSHOT, FULLSIZE_SCREENSHOT, HTML, TEXT, RESOURCETREE, PDF
};
enum BrowsingDataType
{
Expand Down Expand Up @@ -116,6 +116,7 @@ struct IWebDiffWindow
virtual HRESULT Recompare(IWebDiffCallback* callback) = 0;
virtual HRESULT SaveFile(int pane, FormatType kind, const wchar_t* filename, IWebDiffCallback* callback) = 0;
virtual HRESULT SaveFiles(FormatType kind, const wchar_t* filenames[], IWebDiffCallback* callback) = 0;
virtual HRESULT SaveDiffFiles(FormatType kind, const wchar_t* filenames[], IWebDiffCallback* callback) = 0;
virtual HRESULT ClearBrowsingData(int pane, BrowsingDataType datakinds) = 0;
virtual const wchar_t *GetCurrentUrl(int pane) = 0;
virtual int GetPaneCount() const = 0;
Expand Down

0 comments on commit ba22942

Please sign in to comment.