Skip to content

Commit

Permalink
Set document tab icon depending on document type.
Browse files Browse the repository at this point in the history
  • Loading branch information
jovibor committed Nov 9, 2024
1 parent 693e5ee commit f074a19
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
13 changes: 12 additions & 1 deletion Hexer/CChildFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*******************************************************************************/
#include "stdafx.h"
#include "CHexerApp.h"
#include "CChildFrame.h"
#include "CMainFrame.h"
#include "CChildFrame.h"
#include "CHexerDoc.h"
#include "CHexerView.h"

import Utility;
Expand Down Expand Up @@ -37,6 +38,16 @@ void CChildFrame::SetHexerView(CHexerView* pView)

//Private methods.

auto CChildFrame::GetFrameIcon()const->HICON
{
const auto pView = GetHexerView();
if (pView == nullptr) {
return { };
}

return pView->GetDocument()->GetDocIcon();
}

auto CChildFrame::GetMainFrame()const->CMainFrame*
{
return reinterpret_cast<CMainFrame*>(AfxGetMainWnd());
Expand Down
1 change: 1 addition & 0 deletions Hexer/CChildFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CChildFrame final : public CMDIChildWndEx {
[[nodiscard]] auto GetHexerView()const->CHexerView*;
void SetHexerView(CHexerView* pView);
private:
auto GetFrameIcon()const->HICON override;
[[nodiscard]] auto GetMainFrame()const->CMainFrame*;
[[nodiscard]] auto GetHexCtrl()const->HEXCTRL::IHexCtrl*;
afx_msg void OnClose();
Expand Down
10 changes: 3 additions & 7 deletions Hexer/CHexerApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,9 @@ BOOL CHexerApp::InitInstance()
pMainFrame->LoadFrame(IDR_HEXER_FRAME); //Also used in the CMainFrame::PreCreateWindow.
m_pMainWnd = pMainFrame;

const auto iSizeIcon = static_cast<int>(16 * Ut::GetHiDPIInfo().flDPIScale);
const auto hBMPFile = static_cast<HBITMAP>(LoadImageW(AfxGetInstanceHandle(), MAKEINTRESOURCEW(IDB_FILE),
IMAGE_BITMAP, iSizeIcon, iSizeIcon, LR_CREATEDIBSECTION));
const auto hBMPDevice = static_cast<HBITMAP>(LoadImageW(AfxGetInstanceHandle(), MAKEINTRESOURCEW(IDB_DEVICE),
IMAGE_BITMAP, iSizeIcon, iSizeIcon, LR_CREATEDIBSECTION));
const auto hBMPProcess = static_cast<HBITMAP>(LoadImageW(AfxGetInstanceHandle(), MAKEINTRESOURCEW(IDB_PROCESS),
IMAGE_BITMAP, iSizeIcon, iSizeIcon, LR_CREATEDIBSECTION));
const auto hBMPFile = Ut::GetHBITMAP(IDB_FILE);
const auto hBMPDevice = Ut::GetHBITMAP(IDB_DEVICE);
const auto hBMPProcess = Ut::GetHBITMAP(IDB_PROCESS);
MENUITEMINFOW mii { .cbSize { sizeof(MENUITEMINFOW) }, .fMask { MIIM_BITMAP }, .hbmpItem { hBMPFile } };
const auto pFileMenu = pMainFrame->GetMenu()->GetSubMenu(0); //"File" sub-menu.
pFileMenu->SetMenuItemInfoW(1, &mii, TRUE); //Icon for the "Open File..." menu.
Expand Down
21 changes: 21 additions & 0 deletions Hexer/CHexerDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* This software is available under "The Hexer License", see the LICENSE file. *
*******************************************************************************/
#include "stdafx.h"
#include "resource.h"
#include "CHexerApp.h"
#include "CMainFrame.h"
#include "CChildFrame.h"
Expand Down Expand Up @@ -45,6 +46,11 @@ auto CHexerDoc::GetDataSize()const->std::uint64_t
return m_stDataLoader.GetDataSize();
}

auto CHexerDoc::GetDocIcon()const->HICON
{
return m_hDocIcon;
}

auto CHexerDoc::GetMaxVirtOffset()const->std::uint64_t
{
return m_stDataLoader.GetMaxVirtOffset();
Expand Down Expand Up @@ -101,6 +107,21 @@ bool CHexerDoc::OnOpenDocument(const Ut::DATAOPEN& dos)
m_strPathName = GetUniqueDocName(dos).data();
m_bEmbedded = FALSE;
SetTitle(GetDocTitle(dos).data());
const auto iResID = [this]() {
using enum Ut::EOpenMode;
switch (GetOpenMode()) {
case OPEN_FILE:
case NEW_FILE:
return IDB_FILE;
case OPEN_DEVICE:
return IDB_DEVICE;
case OPEN_PROC:
return IDB_PROCESS;
default:
return 0;
}
}();
m_hDocIcon = Ut::HICONfromHBITMAP(Ut::GetHBITMAP(iResID));
m_fOpened = true;

return true;
Expand Down
2 changes: 2 additions & 0 deletions Hexer/CHexerDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CHexerDoc final : public CDocument {
[[nodiscard]] auto GetFileName()const->const std::wstring&;
[[nodiscard]] auto GetDataPath()const->const std::wstring&;
[[nodiscard]] auto GetDataSize()const->std::uint64_t;
[[nodiscard]] auto GetDocIcon()const->HICON;
[[nodiscard]] auto GetMaxVirtOffset()const->std::uint64_t;
[[nodiscard]] auto GetMemPageSize()const->DWORD;
[[nodiscard]] auto GetOpenMode()const->Ut::EOpenMode;
Expand All @@ -41,5 +42,6 @@ class CHexerDoc final : public CDocument {
std::wstring m_wstrDataPath;
std::wstring m_wstrFileName;
Ut::EOpenMode m_eOpenMode { }; //Document open mode.
HICON m_hDocIcon { }; //Document icon.
bool m_fOpened { false }; //Document was successfully opened or not.
};
2 changes: 1 addition & 1 deletion Hexer/CHexerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class CHexerView final : public CView {
public:
[[nodiscard]] auto GetDataInfo()const->Ut::DATAINFO;
[[nodiscard]] auto GetDlgProcMemory()const->HWND;
[[nodiscard]] auto GetDocument()const->CHexerDoc*;
[[nodiscard]] auto GetHexCtrl()const->HEXCTRL::IHexCtrl*;
[[nodiscard]] auto GetHWNDForPane(UINT uPaneID) -> HWND;
private:
[[nodiscard]] auto GetChildFrame()const->CChildFrame*;
[[nodiscard]] auto GetDocument()const->CHexerDoc*;
[[nodiscard]] auto GetMainFrame()const->CMainFrame*;
[[nodiscard]] bool IsPaneAlreadyLaunch(UINT uPaneID)const;
void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)override;
Expand Down
2 changes: 1 addition & 1 deletion Hexer/CMainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ int CMainFrame::OnCreate(LPCREATESTRUCT lpcs)
CMDITabInfo mdiTabParams { };
mdiTabParams.m_style = CMFCTabCtrl::STYLE_3D_ONENOTE; // other styles available...
mdiTabParams.m_bActiveTabCloseButton = TRUE; // set to FALSE to place close button at right of tab area
mdiTabParams.m_bTabIcons = FALSE; //set to TRUE to enable document icons on MDI taba
mdiTabParams.m_bTabIcons = TRUE; //set to TRUE to enable document icons on MDI taba
mdiTabParams.m_bAutoColor = FALSE; //set to FALSE to disable auto-coloring of MDI tabs
mdiTabParams.m_bDocumentMenu = FALSE; //enable the document menu at the right edge of the tab area
mdiTabParams.m_bFlatFrame = TRUE;
Expand Down
19 changes: 19 additions & 0 deletions Hexer/Utility.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ export namespace Ut {
return wstrPath;
};

[[nodiscard]] auto GetHBITMAP(int iResID) -> HBITMAP {
const auto iSizeIcon = static_cast<int>(16 * Ut::GetHiDPIInfo().flDPIScale);
return static_cast<HBITMAP>(LoadImageW(AfxGetInstanceHandle(), MAKEINTRESOURCEW(iResID),
IMAGE_BITMAP, iSizeIcon, iSizeIcon, LR_CREATEDIBSECTION));
}

[[nodiscard]] auto HICONfromHBITMAP(HBITMAP hbmp) -> HICON {
BITMAP stBmp;
if (!GetObjectW(hbmp, sizeof(BITMAP), &stBmp))
return { };

const auto hbmpMask = CreateCompatibleBitmap(::GetDC(nullptr), stBmp.bmWidth, stBmp.bmHeight);
ICONINFO ii { .fIcon { TRUE }, .hbmMask { hbmpMask }, .hbmColor { hbmp } };
auto hICO = CreateIconIndirect(&ii);
DeleteObject(hbmpMask);

return hICO;
}

struct DATAOPEN { //Main data opening struct.
std::wstring wstrDataPath; //Or Process name.
std::uint64_t ullNewFileSize { };
Expand Down

0 comments on commit f074a19

Please sign in to comment.