Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
accelerators view
Browse files Browse the repository at this point in the history
  • Loading branch information
zodiacon committed May 6, 2022
1 parent f87ab6a commit fe39484
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
63 changes: 63 additions & 0 deletions TotalPE/AcceleratorTableView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "pch.h"
#include "AcceleratorTableView.h"
#include "SortHelper.h"

CString CAcceleratorTableView::GetColumnText(HWND, int row, int col) const {
auto& item = m_Items[row];
switch (col) {
case 0: return std::format(L"0x{:X}", item.Id).c_str();
case 1: return std::format(L"0x{:X} ({})", item.Key, KeyFlagsToString(item.Flags)).c_str();
}
return CString();
}

void CAcceleratorTableView::DoSort(SortInfo const* si) {
if (si == nullptr)
return;

auto compare = [&](auto const& item1, auto const& item2) {
switch (si->SortColumn) {
case 0: return SortHelper::Sort(item1.Id, item2.Id, si->SortAscending);
case 1: return SortHelper::Sort(item1.Key, item2.Key, si->SortAscending);
}
return false;
};
std::ranges::sort(m_Items, compare);
}

LRESULT CAcceleratorTableView::OnCreate(UINT, WPARAM, LPARAM, BOOL&) {
m_hWndClient = m_List.Create(*this, rcDefault, nullptr, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
LVS_REPORT | LVS_OWNERDATA, WS_EX_CLIENTEDGE);
m_List.SetExtendedListViewStyle(LVS_EX_DOUBLEBUFFER | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
auto cm = GetColumnManager(m_List);
cm->AddColumn(L"dummy", 0, 10);
cm->AddColumn(L"ID", LVCFMT_RIGHT, 90);
cm->AddColumn(L"Key", LVCFMT_LEFT, 200);
cm->UpdateColumns();
cm->DeleteColumn(0);

return 0;
}

std::wstring CAcceleratorTableView::KeyFlagsToString(DWORD flags) {
std::wstring s;
if (flags & FVIRTKEY)
s += L"VIRTKEY, ";
if (flags & FSHIFT)
s += L"SHIFT, ";
if (flags & FCONTROL)
s += L"CONTROL, ";
if (flags & FALT)
s += L"ALT, ";

return s.empty() ? s : s.substr(0, s.length() - 2);
}

void CAcceleratorTableView::AddAccelTable(std::vector<uint8_t> const& data) {
ATLASSERT(data.size() % sizeof(Accelerator) == 0);
auto count = data.size() / sizeof(Accelerator);
auto start = m_Items.size();
m_Items.resize(start + count);
memcpy(m_Items.data() + start, data.data(), data.size());
m_List.SetItemCount((int)m_Items.size());
}
39 changes: 39 additions & 0 deletions TotalPE/AcceleratorTableView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "View.h"
#include <VirtualListView.h>

class CAcceleratorTableView :
public CView<CAcceleratorTableView>,
public CVirtualListView<CAcceleratorTableView> {
public:
using CView::CView;

void AddAccelTable(std::vector<uint8_t> const& data);

CString GetColumnText(HWND, int row, int col) const;
void DoSort(SortInfo const* si);

BEGIN_MSG_MAP(CAcceleratorTableView)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
CHAIN_MSG_MAP(CView<CAcceleratorTableView>)
CHAIN_MSG_MAP(CVirtualListView<CAcceleratorTableView>)
END_MSG_MAP()

LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

private:
static std::wstring KeyFlagsToString(DWORD flags);

struct Accelerator {
WORD Flags;
WORD Key;
WORD Id;
WORD _padding;
};

CListViewCtrl m_List;
std::vector<Accelerator> m_Items;
};


2 changes: 1 addition & 1 deletion TotalPE/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void CMainFrame::ParseResources(HTREEITEM hRoot, pe_resource_directory_entry con
if (depth == 0) {
if (name.empty())
name = std::format(L"#{}", node.get_id());
type = !name.empty() ? TreeItemType::ResourceTypeName : TreeItemWithIndex(TreeItemType::Resource, node.get_id());
type = !name.empty() ? TreeItemWithIndex(TreeItemType::ResourceTypeName, node.get_id()) : TreeItemWithIndex(TreeItemType::Resource, node.get_id());
icon = ResourceTypeIconIndex(node.get_id());

}
Expand Down
2 changes: 2 additions & 0 deletions TotalPE/TotalPE.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="AboutDlg.cpp" />
<ClCompile Include="AcceleratorTableView.cpp" />
<ClCompile Include="AppSettings.cpp" />
<ClCompile Include="AssemblyView.cpp" />
<ClCompile Include="BufferManager.cpp" />
Expand Down Expand Up @@ -249,6 +250,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="AboutDlg.h" />
<ClInclude Include="AcceleratorTableView.h" />
<ClInclude Include="AppSettings.h" />
<ClInclude Include="AssemblyView.h" />
<ClInclude Include="BufferManager.h" />
Expand Down
6 changes: 6 additions & 0 deletions TotalPE/TotalPE.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
<ClCompile Include="ResourceHelper.cpp">
<Filter>Helpers</Filter>
</ClCompile>
<ClCompile Include="AcceleratorTableView.cpp">
<Filter>Views</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down Expand Up @@ -224,6 +227,9 @@
<ClInclude Include="ResourceHelper.h">
<Filter>Helpers</Filter>
</ClInclude>
<ClInclude Include="AcceleratorTableView.h">
<Filter>Views</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="TotalPE.rc">
Expand Down
15 changes: 14 additions & 1 deletion TotalPE/ViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ExceptionsView.h"
#include "LoadConfigView.h"
#include "ResourceHelper.h"
#include "AcceleratorTableView.h"

ViewManager::ViewManager(IMainFrame* frame) : m_pFrame(frame) {
m_views.reserve(16);
Expand Down Expand Up @@ -134,7 +135,7 @@ HWND ViewManager::CreateOrGetView(TreeItemType type, HWND hParent, pe_image_full
auto section = pe.get_image().get_sections()[DWORD_PTR(type) - DWORD_PTR(TreeItemType::Sections) - 1];
view->SetData(section->get_section_data());
}
if (!hView && type == TreeItemType::ResourceTypeName) {
if (!hView && (type & TreeItemType::ResourceTypeName) == TreeItemType::ResourceTypeName) {
auto typeName = m_pFrame->GetTreeItemText(0);
ResourceHelper rh(pe);
auto res = rh.GetFlatResources(typeName);
Expand All @@ -145,6 +146,13 @@ HWND ViewManager::CreateOrGetView(TreeItemType type, HWND hParent, pe_image_full
view->SetStringTableData(r.Entry->get_data(), _wtoi(r.Name.substr(1).c_str()));
}
}
else if (typeName == L"Accelerators") {
auto view = new CAcceleratorTableView(m_pFrame, pe);
hView = view->DoCreate(hParent);
for (auto const& r : res) {
view->AddAccelTable(r.Entry->get_data());
}
}
}
if (!hView) {
if ((type & TreeItemType::Resource) == TreeItemType::Resource) {
Expand Down Expand Up @@ -173,6 +181,11 @@ HWND ViewManager::CreateOrGetView(TreeItemType type, HWND hParent, pe_image_full
auto sid = m_pFrame->GetTreeItemText(1);
view->SetStringTableData(data, _wtoi(sid.Mid(1)));
}
else if (typeName == L"Accelerators") {
auto view = new CAcceleratorTableView(m_pFrame, pe);
hView = view->DoCreate(hParent);
view->AddAccelTable(data);
}
else {
auto view = new CReadOnlyHexView(m_pFrame);
hView = view->DoCreate(hParent);
Expand Down

0 comments on commit fe39484

Please sign in to comment.