Skip to content

Commit

Permalink
Remove singleton design principles
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jun 6, 2022
1 parent 631ee56 commit f478f0b
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 88 deletions.
20 changes: 15 additions & 5 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ bool pdfv::w::moveWin(HWND hwnd, RECT rect, bool redraw) noexcept
}
}


bool pdfv::w::redraw(HWND hwnd, bool erase) noexcept
{
return ::InvalidateRect(hwnd, nullptr, erase ? TRUE : FALSE) ? true : false;
Expand Down Expand Up @@ -230,7 +231,7 @@ namespace pdfv
switch (wp)
{
case IDOK:
if (textdata != nullptr && textbox != nullptr) [[likely]]
if ((textdata != nullptr) && (textbox != nullptr)) [[likely]]
{
::GetWindowTextW(textbox, textdata, 2048);
}
Expand Down Expand Up @@ -299,7 +300,11 @@ namespace pdfv
nullptr,
nullptr
);
auto hfont{ window.getDefFont() };
// Sorry, no protection here :/
auto pair = static_cast<std::pair<const MainWindow &, wchar_t *> *>(
reinterpret_cast<CREATESTRUCTW *>(lp)->lpCreateParams
);
auto hfont{ pair->first.getDefFont() };
w::setFont(button1, hfont, true);
w::setFont(button2, hfont, true);
w::setFont(messagebox, hfont, true);
Expand All @@ -313,7 +318,7 @@ namespace pdfv
auto s2{ make_xy(w::getWinR(hwnd)) };
w::resize(hwnd, 2 * s2.x - s1.x, 2 * s2.y - s1.y, true);

textdata = static_cast<wchar_t *>(reinterpret_cast<CREATESTRUCTW *>(lp)->lpCreateParams);
textdata = pair->second;
break;
}
case pdfv::WM_MESSAGE:
Expand All @@ -327,7 +332,10 @@ namespace pdfv
}
}

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

Expand All @@ -344,6 +352,8 @@ namespace pdfv
AskProc_finished = false;

wchar_t temp[2048]{};
std::pair<const MainWindow &, wchar_t *> tPair{ window, temp };

auto hwnd = ::CreateWindowExW(
0,
wc.lpszClassName,
Expand All @@ -356,7 +366,7 @@ namespace pdfv
window.getHandle(),
nullptr,
nullptr,
reinterpret_cast<LPVOID>(temp)
static_cast<LPVOID>(&tPair)
);
if (hwnd == nullptr) [[unlikely]]
{
Expand Down
46 changes: 35 additions & 11 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

namespace pdfv
{
// Forward-declare MainWindow class
class MainWindow;

// Some type aliases
using ssize_t = std::intptr_t;
using uchar = unsigned char;
Expand All @@ -50,9 +53,9 @@ namespace pdfv
using ul = unsigned long;
using ll = long long;
using ull = unsigned long long;
using f = float;
using d = double;
using ld = long double;
using f32 = float;
using f64 = double;
using f128 = long double;

using i8 = std::int8_t;
using u8 = std::uint8_t;
Expand Down Expand Up @@ -155,6 +158,23 @@ namespace pdfv
*/
[[nodiscard]] POINT getCur(HWND hwnd, POINT def = {}) noexcept;

/**
* @brief Get the strored pointer information from handle
*
* @param hwnd Window handle
* @return PtrT Retreived pointer with desired type
*/
template<concepts::pointer PtrT>
[[nodiscard]] PtrT getPtr(HWND hwnd) noexcept
{
return reinterpret_cast<PtrT>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA));
}
template<concepts::pointer PtrT>
void setPtr(HWND hwnd, PtrT ptr) noexcept
{
::SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(ptr));
}

/**
* @brief Redraws window
*
Expand Down Expand Up @@ -667,7 +687,7 @@ namespace pdfv
return r;
}

extern xy<f> dpi;
extern xy<f32> dpi;

/**
* @brief Calculates correct screen coordinate using DI pixel and DPI, performs rounding
Expand All @@ -676,9 +696,9 @@ namespace pdfv
* @param dpi DPI scaling factor
* @return int Screen coordinate
*/
[[nodiscard]] constexpr int dip(const int size, const f dpi) noexcept
[[nodiscard]] constexpr int dip(const int size, const f32 dpi) noexcept
{
return int(f(size) * dpi + 0.5f);
return int(f32(size) * dpi + 0.5f);
}
/**
* @brief Calculates correct screen coordinate pair using DI pixel pair and DPI pair, performs rounding
Expand All @@ -687,9 +707,9 @@ namespace pdfv
* @param dpi DPI scaling factor pair
* @return int Screen coordinate pair
*/
[[nodiscard]] constexpr xy<int> dip(const xy<int> size, const xy<f> dpi) noexcept
[[nodiscard]] constexpr xy<int> dip(const xy<int> size, const xy<f32> dpi) noexcept
{
return xy<int>(xy<f>(size) * dpi + 0.5f);
return xy<int>(xy<f32>(size) * dpi + 0.5f);
}

/**
Expand All @@ -699,9 +719,9 @@ namespace pdfv
* @param dpi DPI scaling factor
* @return int Screen coordinate
*/
[[nodiscard]] constexpr int dipfont(const int size, const f dpi) noexcept
[[nodiscard]] constexpr int dipfont(const int size, const f32 dpi) noexcept
{
return -int(f(size) * dpi * 96.0f / 72.0f + 0.5f);
return -int(f32(size) * dpi * 96.0f / 72.0f + 0.5f);
}

/**
Expand All @@ -715,11 +735,15 @@ namespace pdfv
/**
* @brief Asks user info with a given message and window title in a dialog-like window
*
* @param window Constant reference to main window object
* @param message Message to user
* @param title Window title
* @return std::wstring Information string the user entered
*/
[[nodiscard]] std::wstring askInfo(std::wstring_view message, std::wstring_view title) noexcept;
[[nodiscard]] std::wstring askInfo(
const pdfv::MainWindow & window,
std::wstring_view message, std::wstring_view title
) noexcept;

namespace utf
{
Expand Down
8 changes: 4 additions & 4 deletions src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void pdfv::error::report(pdfv::error::Errorcode errid, HWND hwnd) noexcept
}
::MessageBoxW(hwnd, errMsgs[errid], temp, MB_ICONERROR | MB_OK);
}
void pdfv::error::report(pdfv::error::Errorcode errid) noexcept
void pdfv::error::report(pdfv::error::Errorcode errid, const MainWindow & win) noexcept
{
pdfv::error::report(errid, pdfv::window.getHandle());
pdfv::error::report(errid, win.getHandle());
}

pdfv::error::Errorcode pdfv::error::lastErr = pdfv::error::Errorcode::success;
Expand All @@ -49,7 +49,7 @@ void pdfv::error::report(HWND hwnd) noexcept
report(lastErr, hwnd);
}

void pdfv::error::report() noexcept
void pdfv::error::report(const MainWindow & win) noexcept
{
report(lastErr);
report(lastErr, win);
}
8 changes: 6 additions & 2 deletions src/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace pdfv
{
class MainWindow;

namespace error
{
enum Errorcode : int
Expand Down Expand Up @@ -43,17 +45,19 @@ namespace pdfv
* @brief Displays error messages, messagebox owner is the main window
*
* @param errid Error message id
* @param win Window const-reference
*/
void report(Errorcode errid) noexcept;
void report(Errorcode errid, const MainWindow & win) noexcept;

extern Errorcode lastErr;

/**
* @brief Displays error messages based on last error id
* Messagebox owner is the main window
*
* @param win Window const-reference
*/
void report() noexcept;
void report(const MainWindow & win) noexcept;

/**
* @brief Displays error messages based on last error id
Expand Down
28 changes: 20 additions & 8 deletions src/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,21 @@ void pdfv::Pdfium::free() noexcept
s_errorHappened = false;
return error::Errorcode(FPDF_GetLastError() + error::pdf_success);
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(std::string_view path, std::size_t page)
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(
const MainWindow & window,
std::string_view path, std::size_t 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);
return this->pdfLoad(window, utf::conv(path), page);
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const std::wstring & path, std::size_t page)
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(
const MainWindow & window,
const std::wstring & path, std::size_t page
)
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu)\n", static_cast<const void *>(path.c_str()), page);
assert(s_libInit == true);
Expand All @@ -121,14 +127,17 @@ pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const std::wstring & path, std::siz
if (::ReadFile(file, buf, size, &read, nullptr))
{
::CloseHandle(file);
return this->pdfLoad(std::move(buf), std::size_t(read), page);
return this->pdfLoad(window, std::move(buf), std::size_t(read), page);
}

::CloseHandle(file);

return error::pdf_file;
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const u8 * data, std::size_t length, std::size_t page)
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(
const MainWindow & window,
const u8 * data, std::size_t length, std::size_t page
)
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(%p, %zu, %zu)\n", static_cast<const void *>(data), length, page);
assert(s_libInit == true);
Expand All @@ -137,9 +146,12 @@ pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(const u8 * data, std::size_t length
auto buf{ new u8[length] };
std::copy(data, data + length, buf);

return this->pdfLoad(std::move(buf), length, page);
return this->pdfLoad(window, std::move(buf), length, page);
}
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(u8 * && data, std::size_t length, std::size_t page) noexcept
pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(
const MainWindow & window,
u8 * && data, std::size_t length, std::size_t page
) noexcept
{
DEBUGPRINT("pdfv::Pdfium::pdfLoad(&& %p, %zu, %zu)\n", static_cast<void *>(data), length, page);
assert(s_libInit == true);
Expand All @@ -153,7 +165,7 @@ pdfv::error::Errorcode pdfv::Pdfium::pdfLoad(u8 * && data, std::size_t length, s
s_errorHappened = true;
if (auto err{ this->getLastError() }; err == error::pdf_password)
{
auto ans{ utf::conv(askInfo(L"Enter password:", window.getTitle())) };
auto ans{ utf::conv(askInfo(window, L"Enter password:", window.getTitle())) };
this->m_fdoc = FPDF_LoadMemDocument(this->m_buf.get(), length, ans.c_str());
if (this->m_fdoc == nullptr) [[unlikely]]
{
Expand Down
26 changes: 22 additions & 4 deletions src/lib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace pdfv
{
class MainWindow;

class Pdfium
{
private:
Expand Down Expand Up @@ -48,39 +50,55 @@ namespace pdfv
/**
* @brief Loads PDF file from path given as UTF-8 string, loads given page, first page by default
*
* @param window Const-reference to window object
* @param path UTF-8 string path
* @param page Page to load
* @return error::Errorcode
*/
error::Errorcode pdfLoad(std::string_view path, std::size_t page = 1);
error::Errorcode pdfLoad(
const MainWindow & window,
std::string_view path, std::size_t page = 1
);
/**
* @brief Loads PDF file from path given as UTF-16 string, loads given page, first page by default
*
* @param window Const-reference to window object
* @param path UTF-16 string path
* @param page Page to load
* @return error::Errorcode
*/
error::Errorcode pdfLoad(const std::wstring & path, std::size_t page = 1);
error::Errorcode pdfLoad(
const MainWindow & window,
const std::wstring & path, std::size_t page = 1
);
/**
* @brief Loads a PDF file from binary data, given as byte array, preserves the array,
* loads given page, first page by default
*
* @param window Const-reference to window object
* @param data Pointer to an array of bytes containing the PDF
* @param length Length of binary data
* @param page Page to load
* @return error::Errorcode
*/
error::Errorcode pdfLoad(const u8 * data, std::size_t length, std::size_t page = 1);
error::Errorcode pdfLoad(
const MainWindow & window,
const u8 * data, std::size_t length, std::size_t page = 1
);
/**
* @brief Loads a PDF file from binary data, given as byte array, consumes the array,
* loads given page, first page by default
*
* @param window Const-reference to window object
* @param data Pointer to an array of bytes containing the PDF
* @param length Lengths of binary data
* @param page Page to load
* @return error::Errorcode
*/
error::Errorcode pdfLoad(u8 * && data, std::size_t length, std::size_t page = 1) noexcept;
error::Errorcode pdfLoad(
const MainWindow & window,
u8 * && data, std::size_t length, std::size_t page = 1
) noexcept;
/**
* @brief Unloads (closes) currently loaded PDF if any is open,
* also any pages that might be open
Expand Down
Loading

0 comments on commit f478f0b

Please sign in to comment.