Skip to content

Commit

Permalink
Fix bugs, page numbers on status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jan 23, 2022
1 parent 72ea17e commit 6e71d71
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ The x86 (32-bit) Windows binaries can be downloaded [here](https://github.com/ma
# Changelog

* 0.5 (planned)
* [ ] Populate status bar with useful messages
* Fix bug, when closing current tab, the next tab doesn't render
* Show page numbers on status bar
* [ ] Add more status bar functionality
* [ ] Add zooming capability

* 0.4
Expand Down
16 changes: 16 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ void pdfv::w::openWeb(LPCWSTR url) noexcept
::ShellExecuteW(nullptr, L"open", url, nullptr, nullptr, SW_SHOWNORMAL);
}

bool pdfv::w::status::setParts(HWND statusbar, const std::vector<int> & edges) noexcept
{
// Number of edges can't be greater than 256
if (edges.size() > 256) [[unlikely]]
{
return false;
}
return ::SendMessageW(statusbar, SB_SETPARTS, edges.size(), reinterpret_cast<LPARAM>(edges.data())) ? true : false;
}
bool pdfv::w::status::setText(HWND statusbar, int idx, DrawOp drawop, LPCWSTR str) noexcept
{
return ::SendMessageW(statusbar, SB_SETTEXT, idx | int(drawop), reinterpret_cast<LPARAM>(str)) ? true : false;
}




[[nodiscard]] pdfv::ArgVecT pdfv::getArgs(LPWSTR cmdLine, int & argc) noexcept
{
Expand Down
25 changes: 25 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "version.hpp"
#include "resource.hpp"
#include "safeptr.hpp"
#include "enumhelper.hpp"

#define APP_DEFSIZE_X 350
#define APP_DEFSIZE_Y 200
Expand Down Expand Up @@ -257,7 +258,31 @@ namespace pdfv
template<typename T>
using SafeHDC = w::Safeptr<T, decltype(w::DCDeleter)>;

namespace status
{
bool setParts(HWND statusbar, const std::vector<int> & edges) noexcept;

enum class DrawOp : int
{
def = 0,
noBorders = SBT_NOBORDERS,
ownerDraw = SBT_OWNERDRAW,
popOut = SBT_POPOUT,
rtlReading = SBT_RTLREADING,
rightToLeftReading = rtlReading,
noTabParsing = SBT_NOTABPARSING,
};
using DrawingOperation = DrawOp;

bool setText(HWND statusbar, int idx, DrawOp drawop, LPCWSTR str) noexcept;

}
}

// Enable operator overloads on Drawing operations
template<> struct Enumhelper_enabler<pdfv::w::status::DrawOp>
{
};

using ArgVecT = w::Safeptr<
wchar_t **,
Expand Down
10 changes: 10 additions & 0 deletions src/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@ namespace pdfv::concepts
*/
template<typename T>
concept pointer = std::is_pointer_v<T>;


template<typename T>
concept enum_concept = std::is_enum_v<T>;

template<typename T>
concept enum_nonclass = enum_concept<T> && std::is_convertible_v<T, std::underlying_type_t<T>>;

template<typename T>
concept enum_class = enum_concept<T> && !std::is_convertible_v<T, std::underlying_type_t<T>>;
}
39 changes: 39 additions & 0 deletions src/enumhelper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "concepts.hpp"

namespace pdfv
{
template<concepts::enum_concept Enum>
struct Enumhelper_enabler;

template<concepts::enum_concept Enum>
using UnderT = std::underlying_type_t<Enum>;

template<concepts::enum_concept Enum>
[[nodiscard]] constexpr Enum operator|(Enum lhs, Enum rhs) noexcept requires std::is_constructible_v<Enumhelper_enabler<Enum>>
{
return Enum{ UnderT<Enum>(lhs) | UnderT<Enum>(rhs) };
}
template<concepts::enum_concept Enum>
[[nodiscard]] constexpr Enum operator&(Enum lhs, Enum rhs) noexcept requires std::is_constructible_v<Enumhelper_enabler<Enum>>
{
return Enum{ UnderT<Enum>(lhs) & UnderT<Enum>(rhs) };
}
template<concepts::enum_concept Enum>
[[nodiscard]] constexpr Enum operator^(Enum lhs, Enum rhs) noexcept requires std::is_constructible_v<Enumhelper_enabler<Enum>>
{
return Enum{ UnderT<Enum>(lhs) ^ UnderT<Enum>(rhs) };
}

template<concepts::enum_concept Enum>
[[nodiscard]] constexpr bool operator!(Enum lhs) noexcept requires std::is_constructible_v<Enumhelper_enabler<Enum>>
{
return !UnderT<Enum>(lhs);
}
template<concepts::enum_concept Enum>
[[nodiscard]] constexpr Enum operator~(Enum lhs) noexcept requires std::is_constructible_v<Enumhelper_enabler<Enum>>
{
return Enum{ ~UnderT<Enum>(lhs) };
}
}
65 changes: 39 additions & 26 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ pdfv::MainWindow pdfv::MainWindow::mwnd;

[[nodiscard]] bool pdfv::MainWindow::intersectsTabClose() noexcept
{
auto p{ w::getCur(this->m_hwnd) };
auto p{ w::getCur(this->getHandle()) };

for (std::size_t i = 0; i < this->m_tabs.size(); ++i)
{
RECT r;
TabCtrl_GetItemRect(this->m_tabs.m_tabshwnd, i, &r);
TabCtrl_GetItemRect(this->m_tabs.getHandle(), i, &r);
auto closeR{ Tabs::s_calcCloseButton(r) };

if (p.x >= closeR.left && p.x <= closeR.right &&
Expand All @@ -39,6 +39,12 @@ void pdfv::MainWindow::aboutBox() noexcept
);
}

void pdfv::MainWindow::setStatusParts() const noexcept
{
auto w{ this->m_usableArea.x };
w::status::setParts(this->m_statushwnd, { w - dip(200, dpi.x), w - dip(17, dpi.x) });
}

pdfv::MainWindow::MainWindow() noexcept
{
DEBUGPRINT("pdfv::MainWindow::MainWindow()\n");
Expand Down Expand Up @@ -168,7 +174,7 @@ pdfv::MainWindow::~MainWindow() noexcept
this->m_hInst,
const_cast<wchar_t *>(fname)
);
if (this->m_hwnd == nullptr) [[unlikely]]
if (this->getHandle() == nullptr) [[unlikely]]
{
error::lastErr = error::window;
return false;
Expand All @@ -177,20 +183,20 @@ pdfv::MainWindow::~MainWindow() noexcept
// Add "about" to system menu (menu when caption bar is right-clicked)
if (this->m_helpAvailable) [[likely]]
{
auto hSysMenu{ ::GetSystemMenu(this->m_hwnd, false) };
auto hSysMenu{ ::GetSystemMenu(this->getHandle(), false) };
::InsertMenuW(hSysMenu, 5, MF_BYPOSITION | MF_SEPARATOR, 0, nullptr);
::InsertMenuW(hSysMenu, 6, MF_BYPOSITION, IDM_HELP_ABOUT, L"&About");
}
else [[unlikely]]
{
::EnableMenuItem(::GetMenu(this->m_hwnd), IDM_HELP_ABOUT, MF_DISABLED);
::EnableMenuItem(::GetMenu(this->getHandle()), IDM_HELP_ABOUT, MF_DISABLED);
}
this->m_hAccelerators = ::LoadAcceleratorsW(this->m_hInst, MAKEINTRESOURCEW(IDR_ACCELERATOR1));

::ShowWindow(this->m_hwnd, nCmdShow);
::ShowWindow(this->getHandle(), nCmdShow);
// Good practise
::UpdateWindow(this->m_hwnd);
::UpdateWindow(this->getHandle());

return true;
}

Expand Down Expand Up @@ -235,9 +241,9 @@ int pdfv::MainWindow::msgLoop() const noexcept
}
else [[likely]]
{
if (!::TranslateAcceleratorW(this->m_hwnd, this->m_hAccelerators, &msg))
if (!::TranslateAcceleratorW(this->getHandle(), this->m_hAccelerators, &msg))
{
if (!::IsDialogMessageW(this->m_hwnd, &msg))
if (!::IsDialogMessageW(this->getHandle(), &msg))
{
::TranslateMessage(&msg);
::DispatchMessageW(&msg);
Expand All @@ -251,34 +257,34 @@ int pdfv::MainWindow::msgLoop() const noexcept

void pdfv::MainWindow::close() const noexcept
{
if (this->m_hwnd != nullptr) [[likely]]
if (this->getHandle() != nullptr) [[likely]]
{
::SendMessageW(this->m_hwnd, WM_CLOSE, 0, 0);
::SendMessageW(this->getHandle(), WM_CLOSE, 0, 0);
}
}


void pdfv::MainWindow::enable(bool enable) const noexcept
{
DEBUGPRINT("pdfv::MainWindow::enable(%d)\n", enable);
::EnableWindow(this->m_hwnd, enable);
::EnableWindow(this->getHandle(), enable);
}
void pdfv::MainWindow::setTitle(std::wstring_view newTitle)
{
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());
::SetWindowTextW(this->getHandle(), 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", static_cast<const void *>(message), static_cast<const void *>(msgtitle), type);
return ::MessageBoxW(this->m_hwnd, message, msgtitle, type);
return ::MessageBoxW(this->getHandle(), message, msgtitle, type);
}
int pdfv::MainWindow::message(LPCWSTR message, UINT type) const noexcept
{
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);
return ::MessageBoxW(this->getHandle(), message, this->m_title.c_str(), type);
}

LRESULT CALLBACK pdfv::MainWindow::windowProc(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp) noexcept
Expand Down Expand Up @@ -423,7 +429,7 @@ void pdfv::MainWindow::wOnCommand(WPARAM wp) noexcept
switch (LOWORD(wp))
{
case IDM_FILE_OPEN:
if (std::wstring file; this->m_openDialog.open(this->m_hwnd, file))
if (std::wstring file; this->m_openDialog.open(this->getHandle(), file))
{
// Open the PDF
this->openPdfFile(file);
Expand All @@ -436,7 +442,7 @@ void pdfv::MainWindow::wOnCommand(WPARAM wp) noexcept
}
mwnd.m_keyDown[L'W'] = true;
// Close current tab
::SendMessageW(this->m_hwnd, WM_COMMAND, IDM_LIMIT + this->m_tabs.m_tabindex, 0);
::SendMessageW(this->getHandle(), WM_COMMAND, IDM_LIMIT + this->m_tabs.m_tabindex, 0);
break;
case IDM_FILE_EXIT:
this->close();
Expand Down Expand Up @@ -655,13 +661,17 @@ void pdfv::MainWindow::wOnSize() noexcept
DEBUGPRINT("pdfv::MainWindow::wOnSize()\n");
this->m_usableArea = w::getCliR(this->getHandle());


this->m_totalArea = w::getWinR(this->getHandle());
this->m_border = this->m_totalArea - this->m_usableArea;
this->m_border.y -= this->m_menuSize;

auto statusr{ w::getCliR(this->m_statushwnd) };
this->m_tabs.resize(this->m_usableArea - xy<int>{ 0, statusr.bottom - statusr.top });
w::resize(this->m_statushwnd, 0, 0);

// Set status bar parts
this->setStatusParts();
}
void pdfv::MainWindow::wOnCreate(HWND hwnd, LPARAM lp) noexcept
{
Expand Down Expand Up @@ -695,6 +705,9 @@ void pdfv::MainWindow::wOnCreate(HWND hwnd, LPARAM lp) noexcept

clir = w::getCliR(hwnd);
auto statusr{ w::getCliR(this->m_statushwnd) };

this->setStatusParts();

clir.bottom = clir.top + (clir.bottom - clir.top) - (statusr.bottom - statusr.top);

this->m_tabs = Tabs(hwnd, this->getHinst(), clir);
Expand Down Expand Up @@ -765,17 +778,17 @@ void pdfv::MainWindow::wOnCopydata(LPARAM lp) noexcept
void pdfv::MainWindow::wOnBringToFront() noexcept
{
DEBUGPRINT("pdfv::MainWindow::wOnBringToFront()\n");
::ShowWindow(this->m_hwnd, SW_RESTORE);
::ShowWindow(this->getHandle(), SW_RESTORE);
HWND topWnd{ ::GetForegroundWindow() };

DWORD dwMyID { ::GetWindowThreadProcessId(this->m_hwnd, nullptr) };
DWORD dwMyID { ::GetWindowThreadProcessId(this->getHandle(), nullptr) };
DWORD dwCurID{ ::GetWindowThreadProcessId(topWnd, nullptr) };
::AttachThreadInput(dwCurID, dwMyID, TRUE);
::SetWindowPos(this->m_hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(this->m_hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(this->m_hwnd);
::SetFocus(this->m_hwnd);
::SetActiveWindow(this->m_hwnd);
::SetWindowPos(this->getHandle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(this->getHandle(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(this->getHandle());
::SetFocus(this->getHandle());
::SetActiveWindow(this->getHandle());
::AttachThreadInput(dwCurID, dwMyID, FALSE);
}

Expand Down Expand Up @@ -804,7 +817,7 @@ INT_PTR CALLBACK pdfv::MainWindow::aboutProc(HWND hwnd, UINT uMsg, WPARAM wp, LP

// Re-position about box
auto r1{ w::getWinR(hwnd) };
auto r2{ w::getWinR(self->m_hwnd) };
auto r2{ w::getWinR(self->getHandle()) };

// Move dialog to the center of parent window
w::moveWin(
Expand Down
6 changes: 6 additions & 0 deletions src/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace pdfv
void aboutBox() noexcept;
std::wstring m_aboutText{ DEFAULT_ABOUT_TEXT };

void setStatusParts() const noexcept;

public:
//
// A singleton instance of the class supported by a private constructor
Expand Down Expand Up @@ -119,6 +121,10 @@ namespace pdfv
{
return this->m_hwnd;
}
[[nodiscard]] constexpr HWND getStatusHandle() const noexcept
{
return this->m_statushwnd;
}
[[nodiscard]] constexpr HINSTANCE getHinst() const noexcept
{
return this->m_wcex.hInstance;
Expand Down
Loading

0 comments on commit 6e71d71

Please sign in to comment.