Skip to content

Commit

Permalink
GetFont() interface method made const.
Browse files Browse the repository at this point in the history
Added m_dwDigitsOffsetDec and m_dwDigitsOffsetHex for digits amount in dec/hex modes.
  • Loading branch information
jovibor committed May 12, 2024
1 parent 2819b74 commit ae35e60
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 66 deletions.
67 changes: 34 additions & 33 deletions HexCtrl/HexCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ namespace HEXCTRL {
* HEXDATAINFO: Data information used in the IHexVirtData interface. *
********************************************************************************************/
struct HEXDATAINFO {
NMHDR hdr { }; //Standard Windows header.
HEXSPAN stHexSpan { }; //Offset and size of the data bytes.
SpanByte spnData { }; //Data span.
NMHDR hdr { }; //Standard Windows header.
HEXSPAN stHexSpan; //Offset and size of the data bytes.
SpanByte spnData; //Data span.
};

/********************************************************************************************
Expand All @@ -136,7 +136,7 @@ namespace HEXCTRL {
std::wstring wstrDesc; //Bookmark description.
ULONGLONG ullID { }; //Bookmark ID, assigned internally by framework.
ULONGLONG ullData { }; //User defined custom data.
HEXCOLOR stClr { }; //Bookmark bk/text color.
HEXCOLOR stClr; //Bookmark bk/text color.
};
using PHEXBKM = HEXBKM*;

Expand Down Expand Up @@ -180,7 +180,7 @@ namespace HEXCTRL {
struct HEXCOLORINFO {
NMHDR hdr { }; //Standard Windows header.
ULONGLONG ullOffset { }; //Offset for the color.
HEXCOLOR stClr { }; //Colors of the given offset.
HEXCOLOR stClr; //Colors of the given offset.
};

/********************************************************************************************
Expand Down Expand Up @@ -216,16 +216,16 @@ namespace HEXCTRL {

//Template's field main struct.
struct HEXTEMPLFIELD {
std::wstring wstrName; //Field name.
std::wstring wstrDescr; //Field description.
int iOffset { }; //Field offset relative to the Template's beginning.
int iSize { }; //Field size.
HEXCOLOR stClr { }; //Field Bk and Text color.
HexVecFields vecNested; //Vector for nested fields.
PCHEXTEMPLFIELD pFieldParent { }; //Parent field, in case of nested.
EHexFieldType eType { }; //Field type.
std::uint8_t uTypeID { }; //Field type ID if, it's a custom type.
bool fBigEndian { }; //Field endianness.
std::wstring wstrName; //Field name.
std::wstring wstrDescr; //Field description.
int iOffset { }; //Field offset relative to the Template's beginning.
int iSize { }; //Field size.
HEXCOLOR stClr; //Field Bk and Text color.
HexVecFields vecNested; //Vector for nested fields.
PCHEXTEMPLFIELD pFieldParent { }; //Parent field, in case of nested.
EHexFieldType eType { }; //Field type.
std::uint8_t uTypeID { }; //Field type ID if, it's a custom type.
bool fBigEndian { false }; //Field endianness.
};

//Template main struct.
Expand Down Expand Up @@ -276,32 +276,33 @@ namespace HEXCTRL {
COLORREF clrBkCaret { RGB(0, 0, 255) }; //Caret background color.
COLORREF clrBkCaretSel { RGB(0, 0, 200) }; //Caret background color in selection.
};
using PCHEXCOLORS = const HEXCOLORS*;

/********************************************************************************************
* HEXCREATE: Main struct for the HexCtrl creation. *
********************************************************************************************/
struct HEXCREATE {
HWND hWndParent { }; //Parent window handle.
const HEXCOLORS* pColors { }; //HexCtrl colors, nullptr for default.
const LOGFONTW* pLogFont { }; //Monospaced font for HexCtrl, nullptr for default.
RECT rect { }; //Initial window rect.
UINT uID { }; //Control ID if it's a child window.
DWORD dwStyle { }; //Window styles.
DWORD dwExStyle { }; //Extended window styles.
DWORD dwCapacity { 16UL }; //Initial capacity size.
DWORD dwGroupSize { 1UL }; //Initial data grouping size.
float flScrollRatio { 1.0F }; //Either a screen-ratio or lines amount to scroll with Page-scroll (mouse-wheel).
bool fScrollLines { false }; //Treat flScrollRatio as screen-ratio (false) or as amount of lines (true).
bool fInfoBar { true }; //Show bottom Info bar or not.
bool fOffsetHex { true }; //Show offset digits as Hex or Decimal.
bool fCustom { false }; //If it's a custom control in a dialog.
HWND hWndParent { }; //Parent window handle.
PCHEXCOLORS pColors { }; //HexCtrl colors, nullptr for default.
const LOGFONTW* pLogFont { }; //Monospaced font for HexCtrl, nullptr for default.
RECT rect { }; //Initial window rect.
UINT uID { }; //Control ID if it's a child window.
DWORD dwStyle { }; //Window styles.
DWORD dwExStyle { }; //Extended window styles.
DWORD dwCapacity { 16UL }; //Initial capacity size.
DWORD dwGroupSize { 1UL }; //Initial data grouping size.
float flScrollRatio { 1.0F }; //Either a screen-ratio or lines amount to scroll with Page-scroll (mouse-wheel).
bool fScrollLines { false }; //Treat flScrollRatio as screen-ratio (false) or as amount of lines (true).
bool fInfoBar { true }; //Show bottom Info bar or not.
bool fOffsetHex { true }; //Show offset digits as Hex or Decimal.
bool fCustom { false }; //If it's a custom control in a dialog.
};

/********************************************************************************************
* HEXDATA: Main struct for the HexCtrl SetData method. *
********************************************************************************************/
struct HEXDATA {
SpanByte spnData { }; //Data span to display.
SpanByte spnData; //Data span to display.
IHexVirtData* pHexVirtData { }; //Pointer for VirtualData mode.
IHexVirtColors* pHexVirtColors { }; //Pointer for Custom Colors class.
DWORD dwCacheSize { 0x800000UL }; //Data cache size for VirtualData mode.
Expand Down Expand Up @@ -371,8 +372,8 @@ namespace HEXCTRL {
EHexModifyMode eModifyMode { }; //Modify mode.
EHexOperMode eOperMode { }; //Operation mode, used if eModifyMode == MODIFY_OPERATION.
EHexDataType eDataType { }; //Data type of the underlying data, used if eModifyMode == MODIFY_OPERATION.
SpanCByte spnData { }; //Span of the data to modify with.
VecSpan vecSpan { }; //Vector of data offsets and sizes to modify.
SpanCByte spnData; //Span of the data to modify with.
VecSpan vecSpan; //Vector of data offsets and sizes to modify.
bool fBigEndian { false }; //Treat data as the big endian, used if eModifyMode == MODIFY_OPERATION.
};

Expand Down Expand Up @@ -405,7 +406,7 @@ namespace HEXCTRL {
[[nodiscard]] virtual auto GetDataSize()const->ULONGLONG = 0; //Get currently set data size.
[[nodiscard]] virtual auto GetDateInfo()const->std::tuple<DWORD, wchar_t> = 0; //Get date format and separator info.
[[nodiscard]] virtual auto GetDlgItemHandle(EHexWnd eWnd, EHexDlgItem eItem)const->HWND = 0; //Dialogs' items.
[[nodiscard]] virtual auto GetFont() -> LOGFONTW = 0; //Get current font.
[[nodiscard]] virtual auto GetFont()const->LOGFONTW = 0; //Get current font.
[[nodiscard]] virtual auto GetGroupSize()const->DWORD = 0; //Retrieves current data grouping size.
[[nodiscard]] virtual auto GetMenuHandle()const->HMENU = 0; //Context menu handle.
[[nodiscard]] virtual auto GetPagesCount()const->ULONGLONG = 0; //Get count of pages.
Expand Down
73 changes: 44 additions & 29 deletions HexCtrl/src/CHexCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ void CHexCtrl::ClearData()
m_ullCursorPrev = 0;
m_ullCaretPos = 0;
m_ullCursorNow = 0;
m_dwDigitsOffsetDec = 10;
m_dwDigitsOffsetHex = 8;
m_vecUndo.clear();
m_vecRedo.clear();
m_pScrollV->SetScrollPos(0);
Expand Down Expand Up @@ -734,14 +736,15 @@ auto CHexCtrl::GetDlgItemHandle(EHexWnd eWnd, EHexDlgItem eItem)const->HWND
};
}

auto CHexCtrl::GetFont()->LOGFONTW
auto CHexCtrl::GetFont()const->LOGFONTW
{
assert(IsCreated());
if (!IsCreated())
return { };

LOGFONTW lf;
m_fontMain.GetLogFont(&lf);
alignas(4) LOGFONTW lf;
::GetObjectW(m_fontMain.m_hObject, sizeof(LOGFONTW), &lf);

return lf;
}

Expand Down Expand Up @@ -2701,10 +2704,33 @@ void CHexCtrl::SetData(const HEXDATA& hds)

m_spnData = hds.spnData;
m_pHexVirtColors = hds.pHexVirtColors;
m_dwCacheSize = std::clamp(hds.dwCacheSize, 1024 * 64UL, hds.dwCacheSize); //Minimum cache size for VirtualData mode.
m_dwCacheSize = (std::max)(hds.dwCacheSize, 1024UL * 64UL); //Minimum cache size for VirtualData mode.
m_pHexVirtData = hds.pHexVirtData;
m_fMutable = hds.fMutable;
m_fHighLatency = hds.fHighLatency;

const auto ullDataSize = hds.spnData.size();
if (ullDataSize <= 0xffffffffUL) {
m_dwDigitsOffsetDec = 10;
m_dwDigitsOffsetHex = 8;
}
else if (ullDataSize <= 0xffffffffffUL) {
m_dwDigitsOffsetDec = 13;
m_dwDigitsOffsetHex = 10;
}
else if (ullDataSize <= 0xffffffffffffUL) {
m_dwDigitsOffsetDec = 15;
m_dwDigitsOffsetHex = 12;
}
else if (ullDataSize <= 0xffffffffffffffUL) {
m_dwDigitsOffsetDec = 17;
m_dwDigitsOffsetHex = 14;
}
else {
m_dwDigitsOffsetDec = 19;
m_dwDigitsOffsetHex = 16;
}

m_fDataSet = true;

RecalcAll();
Expand Down Expand Up @@ -3503,8 +3529,8 @@ auto CHexCtrl::CopyPrintScreen()const->std::wstring
std::wstring wstrRet;
wstrRet.reserve(static_cast<std::size_t>(ullSelSize) * 4);
wstrRet = L"Offset";
wstrRet.insert(0, (static_cast<std::size_t>(m_dwOffsetDigits) - wstrRet.size()) / 2, ' ');
wstrRet.insert(wstrRet.size(), static_cast<std::size_t>(m_dwOffsetDigits) - wstrRet.size(), ' ');
wstrRet.insert(0, (static_cast<std::size_t>(GetDigitsOffset()) - wstrRet.size()) / 2, ' ');
wstrRet.insert(wstrRet.size(), static_cast<std::size_t>(GetDigitsOffset()) - wstrRet.size(), ' ');
wstrRet += L" "; //Spaces to Capacity.
wstrRet += m_wstrCapacity;
wstrRet += L" "; //Spaces to Text.
Expand Down Expand Up @@ -3722,7 +3748,7 @@ void CHexCtrl::DrawOffsets(CDC* pDC, ULONGLONG ullStartLine, int iLines)const
pDC->SetTextColor(stClrOffset.clrText);
pDC->SetBkColor(stClrOffset.clrBk);
ExtTextOutW(pDC->m_hDC, m_iFirstVertLinePx + GetCharWidthNative() - iScrollH, m_iStartWorkAreaYPx + (m_sizeFontMain.cy * iterLines),
0, nullptr, OffsetToWstr((ullStartLine + iterLines) * dwCapacity).data(), m_dwOffsetDigits, nullptr);
0, nullptr, OffsetToWstr((ullStartLine + iterLines) * dwCapacity).data(), GetDigitsOffset(), nullptr);
}
}

Expand Down Expand Up @@ -4521,11 +4547,16 @@ auto CHexCtrl::GetCommandFromMenu(WORD wMenuID)const->std::optional<EHexCmd>
return std::nullopt;
}

long CHexCtrl::GetFontSize()
long CHexCtrl::GetFontSize()const
{
return GetFont().lfHeight;
}

auto CHexCtrl::GetDigitsOffset()const->DWORD
{
return IsOffsetAsHex() ? m_dwDigitsOffsetHex : m_dwDigitsOffsetDec;
}

auto CHexCtrl::GetRectTextCaption()const->CRect
{
const auto iScrollH { static_cast<int>(m_pScrollH->GetScrollPos()) };
Expand Down Expand Up @@ -4765,7 +4796,8 @@ void CHexCtrl::ModifyWorker(const HEXCTRL::HEXMODIFY& hms, const auto& FuncWorke

auto CHexCtrl::OffsetToWstr(ULONGLONG ullOffset)const->std::wstring
{
return std::vformat(IsOffsetAsHex() ? L"{:0>{}X}" : L"{:0>{}}", std::make_wformat_args(ullOffset, m_dwOffsetDigits));
const auto dwDigitsOffset = GetDigitsOffset();
return std::vformat(IsOffsetAsHex() ? L"{:0>{}X}" : L"{:0>{}}", std::make_wformat_args(ullOffset, dwDigitsOffset));
}

void CHexCtrl::OnCaretPosChange(ULONGLONG ullOffset)
Expand Down Expand Up @@ -4992,32 +5024,14 @@ void CHexCtrl::RecalcAll(CDC* pDC, const CRect* pRect)
}
m_iHeightBottomOffAreaPx = m_iHeightInfoBarPx + m_iIndentBottomLine;

const auto ullDataSize = GetDataSize();
const auto fAsHex = IsOffsetAsHex();
if (ullDataSize <= 0xffffffffUL) {
m_dwOffsetDigits = fAsHex ? 8 : 10;
}
else if (ullDataSize <= 0xffffffffffUL) {
m_dwOffsetDigits = fAsHex ? 10 : 13;
}
else if (ullDataSize <= 0xffffffffffffUL) {
m_dwOffsetDigits = fAsHex ? 12 : 15;
}
else if (ullDataSize <= 0xffffffffffffffUL) {
m_dwOffsetDigits = fAsHex ? 14 : 17;
}
else {
m_dwOffsetDigits = fAsHex ? 16 : 19;
}

const auto dwGroupSize = GetGroupSize();
const auto dwCapacity = GetCapacity();
const auto iCharWidth = GetCharWidthNative();
const auto iCharWidthExt = GetCharWidthExtras();

//Approximately "dwCapacity * 3 + 1" size array of char's width, to be enough for the Hex area chars.
m_vecCharsWidth.assign(dwCapacity * 3 + 1, iCharWidthExt);
m_iSecondVertLinePx = m_iFirstVertLinePx + m_dwOffsetDigits * iCharWidth + iCharWidth * 2;
m_iSecondVertLinePx = m_iFirstVertLinePx + GetDigitsOffset() * iCharWidth + iCharWidth * 2;
m_iSizeHexBytePx = iCharWidthExt * 2;
m_iSpaceBetweenBlocksPx = (dwGroupSize == 1 && dwCapacity > 1) ? iCharWidthExt * 2 : 0;
m_iDistanceGroupedHexChunkPx = m_iSizeHexBytePx * dwGroupSize + iCharWidthExt;
Expand Down Expand Up @@ -5045,6 +5059,7 @@ void CHexCtrl::RecalcAll(CDC* pDC, const CRect* pRect)

//Scrolls, ReleaseDC and Redraw only for current DC, not for PrintingDC.
if (pDC == nullptr) {
const auto ullDataSize = GetDataSize();
m_pScrollV->SetScrollSizes(m_sizeFontMain.cy, GetScrollPageSize(),
static_cast<ULONGLONG>(m_iStartWorkAreaYPx) + m_iHeightBottomOffAreaPx + m_sizeFontMain.cy *
(ullDataSize / dwCapacity + (ullDataSize % dwCapacity == 0 ? 1 : 2)));
Expand Down Expand Up @@ -6022,7 +6037,7 @@ void CHexCtrl::OnMouseMove(UINT nFlags, CPoint point)
TTMainShow(true);
}
}
else if (m_pTFieldTTCurr != nullptr || m_pBkmTTCurr != nullptr) {
else if (m_pTFieldTTCurr != nullptr) {
TTMainShow(false);
}
}
Expand Down
8 changes: 5 additions & 3 deletions HexCtrl/src/CHexCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace HEXCTRL::INTERNAL {
[[nodiscard]] auto GetDataSize()const->ULONGLONG override;
[[nodiscard]] auto GetDateInfo()const->std::tuple<DWORD, wchar_t> override;
[[nodiscard]] auto GetDlgItemHandle(EHexWnd eWnd, EHexDlgItem eItem)const->HWND override;
[[nodiscard]] auto GetFont() -> LOGFONTW override;
[[nodiscard]] auto GetFont()const->LOGFONTW override;
[[nodiscard]] auto GetGroupSize()const->DWORD override;
[[nodiscard]] auto GetMenuHandle()const->HMENU override;
[[nodiscard]] auto GetPagesCount()const->ULONGLONG override;
Expand Down Expand Up @@ -146,7 +146,8 @@ namespace HEXCTRL::INTERNAL {
[[nodiscard]] auto GetCharWidthNative()const->int; //Width of the one char, in px.
[[nodiscard]] auto GetCommandFromKey(UINT uKey, bool fCtrl, bool fShift, bool fAlt)const->std::optional<EHexCmd>; //Get command from keybinding.
[[nodiscard]] auto GetCommandFromMenu(WORD wMenuID)const->std::optional<EHexCmd>; //Get command from menuID.
[[nodiscard]] long GetFontSize();
[[nodiscard]] long GetFontSize()const;
[[nodiscard]] auto GetDigitsOffset()const->DWORD;
[[nodiscard]] auto GetRectTextCaption()const->CRect; //Returns rect of the text caption area.
[[nodiscard]] auto GetScrollPageSize()const->ULONGLONG; //Get the "Page" size of the scroll.
[[nodiscard]] auto GetTopLine()const->ULONGLONG; //Returns current top line number in view.
Expand Down Expand Up @@ -252,7 +253,8 @@ namespace HEXCTRL::INTERNAL {
DWORD m_dwGroupSize { }; //Current data grouping size.
DWORD m_dwCapacity { }; //How many bytes are displayed in one row.
DWORD m_dwCapacityBlockSize { m_dwCapacity / 2 }; //Size of the block before a space delimiter.
DWORD m_dwOffsetDigits { }; //Amount of digits in "Offset", depends on data size set in SetData.
DWORD m_dwDigitsOffsetDec { 10 }; //Amount of digits for "Offset" in Decimal mode, 10 is max for 32bit number.
DWORD m_dwDigitsOffsetHex { 8 }; //Amount of digits for "Offset" in Hex mode, 8 is max for 32bit number.
DWORD m_dwPageSize { 0UL }; //Size of a page to print additional lines between.
DWORD m_dwCacheSize { }; //Data cache size for VirtualData mode.
DWORD m_dwDateFormat { 0xFFFFFFFFUL };//Current date format. See https://docs.microsoft.com/en-gb/windows/win32/intl/locale-idate
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ Returns code page that is currently in use.

### [](#)GetFont
```cpp
[[nodiscard]] auto GetFont()->LOGFONTW;
[[nodiscard]] auto GetFont()const->LOGFONTW;
```
Returns current font's `LOGFONTW`.

Expand Down

0 comments on commit ae35e60

Please sign in to comment.