From f074a19257b3a529794409874ec033a2943ece4b Mon Sep 17 00:00:00 2001 From: Jovibor Date: Sat, 9 Nov 2024 15:25:17 +1000 Subject: [PATCH] Set document tab icon depending on document type. --- Hexer/CChildFrame.cpp | 13 ++++++++++++- Hexer/CChildFrame.h | 1 + Hexer/CHexerApp.cpp | 10 +++------- Hexer/CHexerDoc.cpp | 21 +++++++++++++++++++++ Hexer/CHexerDoc.h | 2 ++ Hexer/CHexerView.h | 2 +- Hexer/CMainFrame.cpp | 2 +- Hexer/Utility.ixx | 19 +++++++++++++++++++ 8 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Hexer/CChildFrame.cpp b/Hexer/CChildFrame.cpp index ccf6b6a..9dcf5a5 100644 --- a/Hexer/CChildFrame.cpp +++ b/Hexer/CChildFrame.cpp @@ -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; @@ -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(AfxGetMainWnd()); diff --git a/Hexer/CChildFrame.h b/Hexer/CChildFrame.h index df02b74..166235d 100644 --- a/Hexer/CChildFrame.h +++ b/Hexer/CChildFrame.h @@ -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(); diff --git a/Hexer/CHexerApp.cpp b/Hexer/CHexerApp.cpp index 716baa8..3e79e39 100644 --- a/Hexer/CHexerApp.cpp +++ b/Hexer/CHexerApp.cpp @@ -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(16 * Ut::GetHiDPIInfo().flDPIScale); - const auto hBMPFile = static_cast(LoadImageW(AfxGetInstanceHandle(), MAKEINTRESOURCEW(IDB_FILE), - IMAGE_BITMAP, iSizeIcon, iSizeIcon, LR_CREATEDIBSECTION)); - const auto hBMPDevice = static_cast(LoadImageW(AfxGetInstanceHandle(), MAKEINTRESOURCEW(IDB_DEVICE), - IMAGE_BITMAP, iSizeIcon, iSizeIcon, LR_CREATEDIBSECTION)); - const auto hBMPProcess = static_cast(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. diff --git a/Hexer/CHexerDoc.cpp b/Hexer/CHexerDoc.cpp index caae3c8..77ec4be 100644 --- a/Hexer/CHexerDoc.cpp +++ b/Hexer/CHexerDoc.cpp @@ -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" @@ -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(); @@ -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; diff --git a/Hexer/CHexerDoc.h b/Hexer/CHexerDoc.h index 7d0f663..6b1a9e9 100644 --- a/Hexer/CHexerDoc.h +++ b/Hexer/CHexerDoc.h @@ -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; @@ -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. }; \ No newline at end of file diff --git a/Hexer/CHexerView.h b/Hexer/CHexerView.h index 6c5f4e6..eb82a94 100644 --- a/Hexer/CHexerView.h +++ b/Hexer/CHexerView.h @@ -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; diff --git a/Hexer/CMainFrame.cpp b/Hexer/CMainFrame.cpp index 1ebbd11..218cd8d 100644 --- a/Hexer/CMainFrame.cpp +++ b/Hexer/CMainFrame.cpp @@ -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; diff --git a/Hexer/Utility.ixx b/Hexer/Utility.ixx index 26e8f50..49d5211 100644 --- a/Hexer/Utility.ixx +++ b/Hexer/Utility.ixx @@ -126,6 +126,25 @@ export namespace Ut { return wstrPath; }; + [[nodiscard]] auto GetHBITMAP(int iResID) -> HBITMAP { + const auto iSizeIcon = static_cast(16 * Ut::GetHiDPIInfo().flDPIScale); + return static_cast(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 { };