Skip to content

Commit

Permalink
Refactor, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jan 19, 2022
1 parent a23f78d commit a7b18e8
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 180 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The x86 (32-bit) Windows binaries can be downloaded [here](https://github.com/ma
* Refactor codebase
* Upgrade PDFium version
* Tabulating using Ctrl+Tab & Ctrl+Shift+Tab
* [ ] Fix most bugs (hopefully)
* Fix most bugs (hopefully)
* [ ] More optimised rendering

* 0.1.0
Expand Down
13 changes: 6 additions & 7 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

std::function<void(wchar_t **)> pdfv::getArgsFree = [](wchar_t ** argVec) noexcept
{
DEBUGPRINT("pdfv::getArgsFree(%p)\n", argVec);
DEBUGPRINT("pdfv::getArgsFree(%p)\n", static_cast<void *>(argVec));
::LocalFree(argVec);
};

[[nodiscard]] std::unique_ptr<wchar_t *, decltype(pdfv::getArgsFree)> pdfv::getArgs(LPWSTR cmdLine, int & argc) noexcept
{
DEBUGPRINT("pdfv::getArgs(%p, %p)\n", cmdLine, &argc);
DEBUGPRINT("pdfv::getArgs(%p, %p)\n", static_cast<void *>(cmdLine), static_cast<void *>(&argc));
wchar_t ** argv = ::CommandLineToArgvW(cmdLine, &argc);
if (argv == nullptr)
{
Expand Down Expand Up @@ -47,7 +47,7 @@ pdfv::xy<float> pdfv::dpi{ 1.0f, 1.0f };

[[nodiscard]] ATOM pdfv::registerClasses(WNDCLASSEXW & wcex) noexcept
{
DEBUGPRINT("pdfv::RegisterClasses(%p)\n", &wcex);
DEBUGPRINT("pdfv::RegisterClasses(%p)\n", static_cast<void *>(&wcex));
if (wcex.cbSize == 0)
{
wcex.cbSize = sizeof(WNDCLASSEXW);
Expand Down Expand Up @@ -87,7 +87,6 @@ namespace pdfv

LRESULT CALLBACK askProc(const HWND hwnd, const UINT uMsg, WPARAM wp, LPARAM lp) noexcept
{
DEBUGPRINT("askProc(%p, %u, %lu %lu)\n", hwnd, uMsg, wp, lp);
static wchar_t * textdata{};
static HWND textbox{}, messagebox{};

Expand Down Expand Up @@ -207,7 +206,7 @@ namespace pdfv

[[nodiscard]] std::wstring pdfv::askInfo(std::wstring_view message, std::wstring_view title) noexcept
{
DEBUGPRINT("pdfv::askInfo(%p, %p)\n", message.data(), title.data());
DEBUGPRINT("pdfv::askInfo(%p, %p)\n", static_cast<const void *>(message.data()), static_cast<const void *>(title.data()));

WNDCLASSEXW wc{ MainWindow::mwnd.getWindowClass() };
wc.lpfnWndProc = &pdfv::askProc;
Expand Down Expand Up @@ -268,7 +267,7 @@ namespace pdfv

[[nodiscard]] std::wstring pdfv::utf::conv(std::string_view str)
{
DEBUGPRINT("pdfv::utf::conv(string_view %p)\n", str.data());
DEBUGPRINT("pdfv::utf::conv(string_view %p)\n", static_cast<const void *>(str.data()));

auto len = ::MultiByteToWideChar(
CP_UTF8,
Expand All @@ -295,7 +294,7 @@ namespace pdfv
}
[[nodiscard]] std::string pdfv::utf::conv(std::wstring_view wstr)
{
DEBUGPRINT("pdfv::utf::conv(wstring_view %p)\n", wstr.data());
DEBUGPRINT("pdfv::utf::conv(wstring_view %p)\n", static_cast<const void *>(wstr.data()));

auto len = ::WideCharToMultiByte(
CP_UTF8,
Expand Down
7 changes: 7 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#pragma once

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#ifndef NOMINMAX
#define NOMINMAX
#endif

#include <windows.h>
#include <commctrl.h>
#include <fpdfview.h>
Expand Down
2 changes: 1 addition & 1 deletion src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const wchar_t * pdfv::error::errMsgs[pdfv::error::max_error]

void pdfv::error::report(pdfv::error::Errorcode errid, HWND hwnd) noexcept
{
DEBUGPRINT("pdfv::error::report(%d, %p)\n", errid, hwnd);
DEBUGPRINT("pdfv::error::report(%d, %p)\n", errid, static_cast<void *>(hwnd));

static const wchar_t * emptyTitle = L"";

Expand Down
20 changes: 12 additions & 8 deletions src/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ pdfv::Pdfium::Pdfium(Pdfium && other) noexcept
m_fpagenum(other.m_fpagenum), m_numPages(other.m_numPages),
m_buf(std::move(other.m_buf))
{
DEBUGPRINT("pdfv::Pdfium::Pdfium(%p)\n", &other);
DEBUGPRINT("pdfv::Pdfium::Pdfium(%p)\n", static_cast<void *>(&other));
other.m_fdoc = nullptr;
other.m_fpage = nullptr;
}
pdfv::Pdfium & pdfv::Pdfium::operator=(Pdfium && other) noexcept
{
DEBUGPRINT("pdfv::Pdfium::operator=(%p)\n", &other);
DEBUGPRINT("pdfv::Pdfium::operator=(%p)\n", static_cast<void *>(&other));

this->m_fdoc = other.m_fdoc;
this->m_fpage = other.m_fpage;
Expand Down Expand Up @@ -57,8 +57,12 @@ void pdfv::Pdfium::init() noexcept
void pdfv::Pdfium::free() noexcept
{
DEBUGPRINT("pdfv::Pdfium::free()\n");
assert(s_libInit == true);
if (s_libInit == false)
{
return;
}

// Free library as usual
FPDF_DestroyLibrary();
s_libInit = false;
}
Expand All @@ -74,15 +78,15 @@ void pdfv::Pdfium::free() noexcept
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(std::string_view path, std::size_t page)
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu)\n", path.data(), page);
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu)\n", static_cast<const void *>(path.data()), page);
assert(s_libInit == true);
this->pdfUnload();

return this->pdfLoad(utf::conv(path), page);
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const std::wstring & path, std::size_t page)
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu)\n", path.c_str(), page);
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu)\n", static_cast<const void *>(path.c_str()), page);
assert(s_libInit == true);
this->pdfUnload();

Expand All @@ -109,7 +113,7 @@ pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const std::wstring & path, std::siz
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const u8 * data, std::size_t length, std::size_t page)
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu, %zu)\n", data, length, page);
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu, %zu)\n", static_cast<const void *>(data), length, page);
assert(s_libInit == true);
this->pdfUnload();

Expand All @@ -120,7 +124,7 @@ pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const u8 * data, std::size_t length
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(u8 * && data, std::size_t length, std::size_t page) noexcept
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(&& %p, %zu, %zu)\n", data, length, page);
DEBUGPRINT("pdfv::Pdfium::pdfLoad(&& %p, %zu, %zu)\n", static_cast<void *>(data), length, page);
assert(s_libInit == true);
this->pdfUnload();

Expand Down Expand Up @@ -202,7 +206,7 @@ void pdfv::Pdfium::pageUnload() noexcept

pdfv::error::Errorcode pdfv::Pdfium::pageRender(HDC dc, pdfv::xy<int> pos, pdfv::xy<int> size) const noexcept
{
DEBUGPRINT("pdf::Pdfium::pageRender(%p, %p, %p)\n", dc, &pos, &size);
DEBUGPRINT("pdf::Pdfium::pageRender(%p, %p, %p)\n", static_cast<void *>(dc), static_cast<void *>(&pos), static_cast<void *>(&size));
assert(s_libInit == true);

if (this->m_fpage != nullptr)
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR cmdLine, int nCmdShow)
{
DEBUGPRINT("wWinMain(%p, , %p, %d)\n", hInst, cmdLine, nCmdShow);
DEBUGPRINT("wWinMain(%p, , %p, %d)\n", static_cast<void *>(hInst), static_cast<void *>(cmdLine), nCmdShow);

int argc;
auto argv = pdfv::getArgs(cmdLine, argc);
Expand Down
45 changes: 24 additions & 21 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void pdfv::MainWindow::aboutBox() noexcept
pdfv::MainWindow::MainWindow() noexcept
{
DEBUGPRINT("pdfv::MainWindow::MainWindow()\n");

pdfv::Pdfium::init();

auto screen = ::GetDC(nullptr);
dpi = {
f(::GetDeviceCaps(screen, LOGPIXELSX)) / 96.0f,
Expand Down Expand Up @@ -95,7 +98,7 @@ pdfv::MainWindow::~MainWindow() noexcept

[[nodiscard]] bool pdfv::MainWindow::init(HINSTANCE hinst, int argc, wchar_t ** argv) noexcept
{
DEBUGPRINT("pdfv::MainWindow::init(%p, %d, %p)\n", hinst, argc, argv);
DEBUGPRINT("pdfv::MainWindow::init(%p, %d, %p)\n", static_cast<void *>(hinst), argc, static_cast<void *>(argv));
this->m_hInst = hinst;
this->m_argc = argc;
this->m_argv = argv;
Expand Down Expand Up @@ -123,7 +126,7 @@ pdfv::MainWindow::~MainWindow() noexcept
return false;
}

this->m_wcex.lpfnWndProc = &pdfv::TabObject::tabProc;
this->m_wcex.lpfnWndProc = &pdfv::TabObject::tabProcHub;
this->m_wcex.lpszClassName = APP_CLASSNAME "Tab";
this->m_wcex.lpszMenuName = nullptr;
this->m_wcex.hbrBackground = ::CreateSolidBrush(RGB(255, 255, 255));
Expand All @@ -149,7 +152,7 @@ pdfv::MainWindow::~MainWindow() noexcept

[[nodiscard]] bool pdfv::MainWindow::run(const wchar_t * fname, int nCmdShow) noexcept
{
DEBUGPRINT("pdfv::MainWindow::run(%p, %d)\n", fname, nCmdShow);
DEBUGPRINT("pdfv::MainWindow::run(%p, %d)\n", static_cast<const void *>(fname), nCmdShow);
this->m_hwnd = ::CreateWindowExW(
0,
APP_CLASSNAME,
Expand Down Expand Up @@ -251,19 +254,19 @@ void pdfv::MainWindow::enable(bool enable) const noexcept
}
void pdfv::MainWindow::setTitle(std::wstring_view newTitle)
{
DEBUGPRINT("pdfv::MainWindow::setTitle(%p)\n", newTitle.data());
DEBUGPRINT("pdfv::MainWindow::setTitle(%p)\n", static_cast<const void *>(newTitle.data()));
this->m_title = newTitle;
::SetWindowTextW(this->m_hwnd, this->m_title.c_str());
}

int pdfv::MainWindow::message(LPCWSTR message, LPCWSTR msgtitle, UINT type) const noexcept
{
DEBUGPRINT("pdfv::MainWindow::message(%p, %p, %u)\n", message, msgtitle, type);
DEBUGPRINT("pdfv::MainWindow::message(%p, %p, %u)\n", static_cast<const void *>(message), static_cast<const void *>(msgtitle), type);
return ::MessageBoxW(this->m_hwnd, message, msgtitle, type);
}
int pdfv::MainWindow::message(LPCWSTR message, UINT type) const noexcept
{
DEBUGPRINT("pdfv::MainWindow::message(%p, %u)\n", message, type);
DEBUGPRINT("pdfv::MainWindow::message(%p, %u)\n", static_cast<const void *>(message), type);
return ::MessageBoxW(this->m_hwnd, message, this->m_title.c_str(), type);
}

Expand Down Expand Up @@ -315,7 +318,7 @@ LRESULT CALLBACK pdfv::MainWindow::windowProc(const HWND hwnd, const UINT uMsg,

void pdfv::MainWindow::wOnCommand(WPARAM wp) noexcept
{
DEBUGPRINT("pdfv::MainWindow::onCommand(%lu)\n", wp);
DEBUGPRINT("pdfv::MainWindow::onCommand(%u)\n", wp);
switch (LOWORD(wp))
{
case IDM_FILE_OPEN:
Expand All @@ -341,8 +344,8 @@ void pdfv::MainWindow::wOnCommand(WPARAM wp) noexcept
else
{
auto it = this->m_tabs.rename(Tabs::defaulttitle);
it->second->pdfUnload();
it->updatePDF();
(*it)->second.pdfUnload();
(*it)->updatePDF();
}
}
break;
Expand Down Expand Up @@ -398,17 +401,17 @@ void pdfv::MainWindow::wOnCommand(WPARAM wp) noexcept
else
{
auto it = this->m_tabs.rename(Tabs::defaulttitle);
it->second->pdfUnload();
it->updatePDF();
(*it)->second.pdfUnload();
(*it)->updatePDF();
}
}
}
}
}
void pdfv::MainWindow::wOnKeydown(WPARAM wp) noexcept
{
DEBUGPRINT("pdfv::MainWindow::wOnKeyDown(%lu)\n", wp);
auto target{ mwnd.m_tabs.m_tabs[mwnd.m_tabs.m_tabindex].tabhandle };
DEBUGPRINT("pdfv::MainWindow::wOnKeyDown(%u)\n", wp);
auto target{ mwnd.m_tabs.m_tabs[mwnd.m_tabs.m_tabindex]->tabhandle };
switch (wp)
{
case VK_HOME:
Expand All @@ -427,7 +430,7 @@ void pdfv::MainWindow::wOnKeydown(WPARAM wp) noexcept
}
void pdfv::MainWindow::wOnMousewheel(WPARAM wp) noexcept
{
DEBUGPRINT("pdfv::MainWindow::wOnMousewheel(%lu)\n", wp);
DEBUGPRINT("pdfv::MainWindow::wOnMousewheel(%u)\n", wp);
static int delta = 0;
delta += int(GET_WHEEL_DELTA_WPARAM(wp));

Expand All @@ -445,7 +448,7 @@ void pdfv::MainWindow::wOnMousewheel(WPARAM wp) noexcept
{
for (int i = std::abs(delta); i > 0; i -= WHEEL_DELTA)
::SendMessageW(
mwnd.m_tabs.m_tabs[mwnd.m_tabs.m_tabindex].tabhandle,
mwnd.m_tabs.m_tabs[mwnd.m_tabs.m_tabindex]->tabhandle,
WM_VSCROLL,
MAKELONG(dir, 0),
0
Expand Down Expand Up @@ -481,7 +484,7 @@ void pdfv::MainWindow::wOnMove(LPARAM lp) noexcept
}
void pdfv::MainWindow::wOnSizing(WPARAM wp, LPARAM lp) noexcept
{
DEBUGPRINT("pdfv::MainWindow::wOnSizing(%lu, %lu)\n", wp, lp);
DEBUGPRINT("pdfv::MainWindow::wOnSizing(%u, %lu)\n", wp, lp);
auto r = reinterpret_cast<RECT*>(lp);
if ((r->right - r->left) < mwnd.m_minArea.x)
{
Expand Down Expand Up @@ -532,7 +535,7 @@ void pdfv::MainWindow::wOnSize() noexcept
}
void pdfv::MainWindow::wOnCreate(HWND hwnd, LPARAM lp) noexcept
{
DEBUGPRINT("pdfv::MainWindow::wOnCreate(%p, %lu)\n", hwnd, lp);
DEBUGPRINT("pdfv::MainWindow::wOnCreate(%p, %lu)\n", static_cast<void *>(hwnd), lp);
mwnd.m_hwnd = hwnd;
{
RECT r1{}, r2{};
Expand Down Expand Up @@ -649,7 +652,7 @@ LRESULT CALLBACK pdfv::MainWindow::aboutProc(HWND hwnd, UINT uMsg, WPARAM wp, LP

void pdfv::MainWindow::openPdfFile(std::wstring_view file) noexcept
{
DEBUGPRINT("pdfv::MainWindow::openPdfFile(%p)\n", file.data());
DEBUGPRINT("pdfv::MainWindow::openPdfFile(%p)\n", static_cast<const void *>(file.data()));
std::wstring_view fshort;
for (std::size_t i = file.length() - 1; i > 0; --i)
{
Expand All @@ -659,7 +662,7 @@ void pdfv::MainWindow::openPdfFile(std::wstring_view file) noexcept
break;
}
}
std::vector<pdfv::TabObject>::iterator it;
pdfv::Tabs::listtype::iterator it;
if (this->m_tabs.size() == 1 && this->m_tabs.getName() == Tabs::defaulttitlepadded)
{
it = this->m_tabs.rename(fshort);
Expand All @@ -669,8 +672,8 @@ void pdfv::MainWindow::openPdfFile(std::wstring_view file) noexcept
it = this->m_tabs.insert(fshort);
}

it->second->pdfLoad(std::wstring(file));
it->updatePDF();
(*it)->second.pdfLoad(std::wstring(file));
(*it)->updatePDF();

this->m_tabs.select();
}
Loading

0 comments on commit a7b18e8

Please sign in to comment.