Skip to content

Commit

Permalink
Const correctness.
Browse files Browse the repository at this point in the history
Docs.
  • Loading branch information
jovibor committed Oct 8, 2022
1 parent 2fc4ccf commit 88071b9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 63 deletions.
16 changes: 8 additions & 8 deletions HexCtrl/src/CHexCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ bool CHexCtrl::Create(const HEXCREATE& hcs)
m_penLines.CreatePen(PS_SOLID, 1, RGB(200, 200, 200));

//Removing window's border frame.
MARGINS marg { 0, 0, 0, 1 };
const MARGINS marg { 0, 0, 0, 1 };
DwmExtendFrameIntoClientArea(m_hWnd, &marg);
SetWindowPos(nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);

Expand Down Expand Up @@ -351,7 +351,7 @@ bool CHexCtrl::CreateDialogCtrl(UINT uCtrlID, HWND hWndParent)
if (IsCreated())
return false;

HEXCREATE hcs { .hWndParent { hWndParent }, .uID { uCtrlID }, .dwStyle { WS_VISIBLE | WS_CHILD }, .fCustom { true } };
const HEXCREATE hcs { .hWndParent { hWndParent }, .uID { uCtrlID }, .dwStyle { WS_VISIBLE | WS_CHILD }, .fCustom { true } };
return Create(hcs);
}

Expand Down Expand Up @@ -1089,7 +1089,7 @@ void CHexCtrl::ModifyData(const HEXMODIFY& hms)
//Then clone this buffer to the destination data.
//Buffer is allocated with alignment for maximum performance.
constexpr auto ulSizeRandBuff = 1024U * 1024U; //1MB.
std::unique_ptr < std::byte[], decltype([](std::byte* pData) { _aligned_free(pData); }) >
const std::unique_ptr < std::byte[], decltype([](std::byte* pData) { _aligned_free(pData); }) >
uptrRandData(static_cast<std::byte*>(_aligned_malloc(ulSizeRandBuff, 32)));

for (auto iter = 0UL; iter < ulSizeRandBuff; iter += sizeof(std::uint64_t)) {
Expand Down Expand Up @@ -2243,7 +2243,7 @@ void CHexCtrl::ClipboardPaste(EClipboard eType)
break;
case EClipboard::PASTE_HEX:
{
const auto optData = NumStrToHex(pDataClpbrd);
auto optData = NumStrToHex(pDataClpbrd);
if (!optData) {
GlobalUnlock(hClpbrd);
CloseClipboard();
Expand Down Expand Up @@ -3714,7 +3714,7 @@ void CHexCtrl::OnCaretPosChange(ULONGLONG ullOffset)

if (auto pBkm = m_pBookmarks->HitTest(ullOffset); pBkm != nullptr) //If clicked on bookmark.
{
HEXBKMINFO hbi { .hdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), HEXCTRL_MSG_BKMCLICK }, .pBkm { pBkm } };
const HEXBKMINFO hbi { .hdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), HEXCTRL_MSG_BKMCLICK }, .pBkm { pBkm } };
ParentNotify(hbi);
}

Expand All @@ -3729,7 +3729,7 @@ void CHexCtrl::ParentNotify(const T& t)const

void CHexCtrl::ParentNotify(UINT uCode)const
{
NMHDR nmhdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), uCode };
const NMHDR nmhdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), uCode };
ParentNotify(nmhdr);
}

Expand Down Expand Up @@ -4580,7 +4580,7 @@ BOOL CHexCtrl::OnCommand(WPARAM wParam, LPARAM lParam)
}
else
{ //For user defined custom menu we notifying parent window.
HEXMENUINFO hmi { .hdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), HEXCTRL_MSG_MENUCLICK },
const HEXMENUINFO hmi { .hdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), HEXCTRL_MSG_MENUCLICK },
.pt { m_stMenuClickedPt }, .wMenuID { wMenuID } };
ParentNotify(hmi);
}
Expand All @@ -4591,7 +4591,7 @@ BOOL CHexCtrl::OnCommand(WPARAM wParam, LPARAM lParam)
void CHexCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
//Notify parent that we are about to display a context menu.
HEXMENUINFO hmi { .hdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), HEXCTRL_MSG_CONTEXTMENU }, .pt { m_stMenuClickedPt = point } };
const HEXMENUINFO hmi { .hdr { m_hWnd, static_cast<UINT>(GetDlgCtrlID()), HEXCTRL_MSG_CONTEXTMENU }, .pt { m_stMenuClickedPt = point } };
ParentNotify(hmi);
m_menuMain.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON, point.x, point.y, this);
}
Expand Down
2 changes: 1 addition & 1 deletion HexCtrl/src/Dialogs/CHexDlgFillData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void CHexDlgFillData::OnOK()
{
case EFillType::FILL_HEX:
{
const auto optData = NumStrToHex(wstrFillWith);
auto optData = NumStrToHex(wstrFillWith);
if (!optData) {
MessageBoxW(L"Wrong Hex format!", L"Format Error", MB_ICONERROR);
return;
Expand Down
4 changes: 2 additions & 2 deletions HexCtrl/src/Dialogs/CHexDlgSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ BOOL CHexDlgSearch::Create(UINT nIDTemplate, CWnd* pParent, IHexCtrl* pHexCtrl)
bool CHexDlgSearch::IsSearchAvail()const
{
const auto* const pHexCtrl = GetHexCtrl();
return !(m_wstrTextSearch.empty() || !pHexCtrl->IsDataSet() || m_ullOffsetCurr >= pHexCtrl->GetDataSize());
return pHexCtrl->IsDataSet() && !m_wstrTextSearch.empty() && m_ullOffsetCurr < pHexCtrl->GetDataSize();
}

void CHexDlgSearch::SearchNextPrev(bool fForward)
Expand Down Expand Up @@ -579,7 +579,7 @@ BOOL CHexDlgSearch::OnCommand(WPARAM wParam, LPARAM lParam)
for (auto i = 0UL; i < m_pListMain->GetSelectedCount(); ++i)
{
nItem = m_pListMain->GetNextItem(nItem, LVNI_SELECTED);
HEXBKM hbs { .vecSpan = { HEXSPAN { m_vecSearchRes.at(static_cast<size_t>(nItem)),
const HEXBKM hbs { .vecSpan = { HEXSPAN { m_vecSearchRes.at(static_cast<size_t>(nItem)),
m_fReplace ? m_spnReplace.size() : m_spnSearch.size() } }, .wstrDesc = m_wstrTextSearch };
GetHexCtrl()->GetBookmarks()->AddBkm(hbs, false);
}
Expand Down
48 changes: 24 additions & 24 deletions HexCtrl/src/Dialogs/CHexDlgSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,39 @@ namespace HEXCTRL::INTERNAL
CEdit m_stEditLimit; //Edit box "Limit search hit".
CBrush m_stBrushDefault;
const COLORREF m_clrBkTextArea { GetSysColor(COLOR_MENU) };
ULONGLONG m_ullBoundBegin { }; //Search start boundary.
ULONGLONG m_ullBoundEnd { }; //Search end boundary.
ULONGLONG m_ullOffsetCurr { }; //Current offset a search should start from.
ULONGLONG m_ullSizeSentinel { };//Maximum size that search can't cross.
ULONGLONG m_ullStep { 1 }; //Search step (default is 1 byte).
DWORD m_dwCount { }; //How many, or what index number.
DWORD m_dwReplaced { }; //Replaced amount;
DWORD m_dwFoundLimit { 10000 }; //Maximum found search occurences.
int m_iDirection { }; //Search direction: 1 = Forward, -1 = Backward.
int m_iWrap { }; //Wrap direction: -1 = Beginning, 1 = End.
ULONGLONG m_ullBoundBegin { }; //Search start boundary.
ULONGLONG m_ullBoundEnd { }; //Search end boundary.
ULONGLONG m_ullOffsetCurr { }; //Current offset a search should start from.
ULONGLONG m_ullSizeSentinel { }; //Maximum size that search can't cross.
ULONGLONG m_ullStep { 1 }; //Search step (default is 1 byte).
DWORD m_dwCount { }; //How many, or what index number.
DWORD m_dwReplaced { }; //Replaced amount;
DWORD m_dwFoundLimit { 10000 }; //Maximum found search occurences.
int m_iDirection { }; //Search direction: 1 = Forward, -1 = Backward.
int m_iWrap { }; //Wrap direction: -1 = Beginning, 1 = End.
std::span<std::byte> m_spnSearch; //"Search" span.
std::span<std::byte> m_spnReplace; //"Replace" span.
std::string m_strSearch; //Actual string to search after all conversions.
std::string m_strReplace; //Actual string to replace.
std::wstring m_wstrSearch; //Actual wstring to search.
std::wstring m_wstrReplace; //Actual wstring to replace.
std::wstring m_wstrTextSearch { }; //Text from "Search" box.
std::wstring m_wstrTextReplace { }; //Text from "Replace with..." box.
std::wstring m_wstrTextSearch; //Text from "Search" box.
std::wstring m_wstrTextReplace; //Text from "Replace with..." box.
std::wstring_view m_wstrWrongInput { L"Wrong input data!" };
HEXSPAN m_stSelSpan { }; //Previous selection.
void(CHexDlgSearch::*m_pfnThread)(STHREADRUN* pThread); //Func pointer to the ThreadRun<> for the Search thread.
const std::byte m_uWildcard { '?' }; //Wildcard symbol.
bool m_fSecondMatch { false }; //First or subsequent match.
bool m_fFound { false }; //Found or not.
bool m_fDoCount { true }; //Do we count matches or just print "Found".
bool m_fReplace { false }; //Find or Find and Replace with...?
bool m_fAll { false }; //Find/Replace one by one, or all?
bool m_fSelection { false }; //"In selection" check box.
bool m_fWildcard { false }; //"Wildcard" check box.
bool m_fBigEndian { false }; //"Big-endian" check box.
bool m_fMatchCase { false }; //"Match case" check box.
bool m_fInverted { false }; //"Inverted" check box
bool m_fReplaceWarn { true }; //Show "Replace string size exceeds..." warning message or not.
bool m_fSearchNext { false }; //Search through Next/Prev menu.
bool m_fSecondMatch { false }; //First or subsequent match.
bool m_fFound { false }; //Found or not.
bool m_fDoCount { true }; //Do we count matches or just print "Found".
bool m_fReplace { false }; //Find or Find and Replace with...?
bool m_fAll { false }; //Find/Replace one by one, or all?
bool m_fSelection { false }; //"In selection" check box.
bool m_fWildcard { false }; //"Wildcard" check box.
bool m_fBigEndian { false }; //"Big-endian" check box.
bool m_fMatchCase { false }; //"Match case" check box.
bool m_fInverted { false }; //"Inverted" check box
bool m_fReplaceWarn { true }; //Show "Replace string size exceeds..." warning message or not.
bool m_fSearchNext { false }; //Search through Next/Prev menu.
};
}
43 changes: 15 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,11 @@
* [GetHexCtrlInfo](#gethexctrlinfo)
* [HEXCTRLINFO](#hexctrlinfo)
</details>
* [Positioning and Sizing](#positioning-and-sizing)
* [Appearance](#appearance)
* [Licensing](#licensing)

## [](#)Introduction
**HexCtrl** is a very featured hex viewer/editor control written in **C++** with the help of the **MFC** library.
**HexCtrl** is a very featured hex viewer/editor control written in **C++/MFC**.

It's implemented as a pure abstract interface and therefore can be used in your app even if you don't use **MFC** directly. It's written with **/std:c++20** standard in **Visual Studio 2022**.

Expand All @@ -140,34 +139,33 @@ It's implemented as a pure abstract interface and therefore can be used in your
## [](#)Installation
The **HexCtrl** can be used in two different ways:
* Building from the sources as a part of your project
* Using as a *.dll*.
* Using as a Dynamic Link Library *.dll*.

### [](#)Building From The Sources
The building process is quite simple:
1. Copy *HexCtrl* folder into your project's directory.
2. Add all files from the *HexCtrl* folder into your project, except for
*HexCtrl/dep/rapidjson/rapidjson-amalgam.h* (header-only lib).
3. Add `#include "HexCtrl/HexCtrl.h"` where you suppose to use the **HexCtrl**.
4. Declare [**HexCtrl**'s namespace](#namespace): `using namespace HEXCTRL;`
5. Declare `IHexCtrlPtr` member variable: `IHexCtrlPtr myHex { CreateHexCtrl() };`
6. [Create](#creating) control instance.
1. Add all files from the *HexCtrl* folder into your project (you can skip
*HexCtrl/dep/rapidjson/rapidjson-amalgam.h* header-only lib).
1. Add `#include "HexCtrl/HexCtrl.h"` where you suppose to use the **HexCtrl**.
1. Declare `IHexCtrlPtr` member variable: `IHexCtrlPtr myHex { HEXCTRL::CreateHexCtrl() };`
1. [Create](#creating) control instance.

If you want to build **HexCtrl** from the sources in non **MFC** app you will have to:
1. Add support for **Use MFC in a Shared DLL** in your project settings.
2. Uncomment the line `//#define HEXCTRL_MANUAL_MFC_INIT` in `HexCtrl.h` header file.
1. Uncomment the line `//#define HEXCTRL_MANUAL_MFC_INIT` in `HexCtrl.h` header file.

### [](#)Dynamic Link Library
To use **HexCtrl** as the *.dll* do the following:
1. Copy *HexCtrl.h* and *HexCtrlDefs.h* files into your project's folder.
2. Copy *HexCtrl.lib* file into your project's folder, so that linker can see it.
3. Put *HexCtrl.dll* file next to your *.exe* file.
4. Add the following line where you suppose to use the control:
1. Copy *HexCtrl.lib* file into your project's folder, so that linker can see it.
1. Put *HexCtrl.dll* file next to your *.exe* file.
1. Add the following line where you suppose to use the control:
```cpp
#define HEXCTRL_SHARED_DLL //You can alternatively uncomment this line in HexCtrl.h.
#include "HexCtrl.h"`
```
5. Declare `IHexCtrlPtr` member variable: `IHexCtrlPtr myHex { CreateHexCtrl() };`
6. [Create](#creating) control instance.
1. Declare `IHexCtrlPtr` member variable: `IHexCtrlPtr myHex { HEXCTRL::CreateHexCtrl() };`
1. [Create](#creating) control instance.
To build *HexCtrl.dll* and *HexCtrl.lib* use the *DLL Project/DLL Project.vcxproj* **Visual Studio** project file.
Expand All @@ -177,8 +175,7 @@ To build *HexCtrl.dll* and *HexCtrl.lib* use the *DLL Project/DLL Project.vcxpro
Building **HexCtrl** with **MFC Shared DLL** turned out to be a little tricky. Even with the help of `AFX_MANAGE_STATE(AfxGetStaticModuleState())` macro there always were **MFC** debug assertions, which origins quite hard to comprehend.
### [](#)Namespace
**HexCtrl** uses its own namespace `HEXCTRL`.
So it's up to you, whether to use namespace prefix before declarations:
**HexCtrl** uses its own namespace `HEXCTRL`. So it's up to you, whether to use namespace prefix before declarations:
```cpp
HEXCTRL::
```
Expand All @@ -190,7 +187,7 @@ using namespace HEXCTRL;
## [](#)Creating
### [](#)Classic Approach
The [`Create`](#create) method is the first method you call to create **HexCtrl** instance. It takes [`HEXCREATE`](#hexcreate) struct as an argument which provides all necessary information for the creation process.
The [`Create`](#create) method is the first method you call to create **HexCtrl** instance. It takes [`HEXCREATE`](#hexcreate) struct as an argument which provides all necessary information for the control.
The `HEXCREATE::dwStyle` and `HEXCREATE::dwExStyle` are [Window](https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles) and [Extended Window](https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles) styles respectively, they will be passed to the **HexCtrl**'s window creating function, set these styles according to your needs.
For all available creation options see the [`HEXCREATE`](#hexcreate) struct description.

Expand Down Expand Up @@ -1013,16 +1010,6 @@ struct HEXCTRLINFO
};
```

## [](#)Positioning and Sizing
To properly resize and position your **HexCtrl**'s window you may have to handle `WM_SIZE` message in its parent window, in something like this way:
```cpp
void CMyWnd::OnSize(UINT nType, int cx, int cy)
{
//...
::SetWindowPos(m_myHex->GetWindowHandle(EHexWnd::WND_MAIN), this->m_hWnd, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER);
}
```
## [](#)Appearance
To change control's font size — <kbd>Ctrl+MouseWheel</kbd>
To change control's capacity — <kbd>Ctrl+Shift+MouseWheel</kbd>
Expand Down

0 comments on commit 88071b9

Please sign in to comment.