1
0
mirror of synced 2024-12-02 11:17:19 +01:00
ImHex/plugins/builtin/source/content/views/view_bookmarks.cpp

182 lines
7.6 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include "content/views/view_bookmarks.hpp"
#include <hex/providers/provider.hpp>
2021-08-29 22:15:18 +02:00
#include <hex/helpers/fmt.hpp>
2021-12-07 22:47:41 +01:00
#include <hex/helpers/project_file_handler.hpp>
#include <cstring>
2021-12-07 22:47:41 +01:00
namespace hex::plugin::builtin {
2021-12-07 22:47:41 +01:00
ViewBookmarks::ViewBookmarks() : View("hex.builtin.view.bookmarks.name") {
EventManager::subscribe<RequestAddBookmark>(this, [this](Region region, std::string name, std::string comment, color_t color) {
if (name.empty()) {
name = hex::format("hex.builtin.view.bookmarks.default_title"_lang, region.address, region.address + region.size - 1);
}
if (color == 0x00)
color = ImGui::GetColorU32(ImGuiCol_Header);
2022-02-01 22:09:44 +01:00
this->m_bookmarks.push_back({ region,
name,
std::move(comment),
color,
false,
2022-02-01 22:09:44 +01:00
ImHexApi::HexEditor::addHighlight(region, color, name) });
2020-11-30 00:03:12 +01:00
ProjectFile::markDirty();
});
EventManager::subscribe<EventProjectFileLoad>(this, [this] {
this->m_bookmarks = ProjectFile::getBookmarks();
2020-11-30 00:03:12 +01:00
});
EventManager::subscribe<EventProjectFileStore>(this, [this] {
ProjectFile::setBookmarks(this->m_bookmarks);
});
EventManager::subscribe<EventFileUnloaded>(this, [this] {
this->m_bookmarks.clear();
});
}
ViewBookmarks::~ViewBookmarks() {
EventManager::unsubscribe<RequestAddBookmark>(this);
EventManager::unsubscribe<EventProjectFileLoad>(this);
EventManager::unsubscribe<EventProjectFileStore>(this);
EventManager::unsubscribe<EventFileUnloaded>(this);
this->m_bookmarks.clear();
}
void ViewBookmarks::drawContent() {
2021-12-07 22:47:41 +01:00
if (ImGui::Begin(View::toWindowName("hex.builtin.view.bookmarks.name").c_str(), &this->getWindowOpenState())) {
if (ImGui::BeginChild("##scrolling")) {
if (this->m_bookmarks.empty()) {
ImGui::TextFormattedCentered("hex.builtin.view.bookmarks.no_bookmarks"_lang);
2021-01-08 19:34:29 +01:00
}
2022-02-01 22:09:44 +01:00
u32 id = 1;
auto bookmarkToRemove = this->m_bookmarks.end();
for (auto iter = this->m_bookmarks.begin(); iter != this->m_bookmarks.end(); iter++) {
auto &[region, name, comment, color, locked, highlight] = *iter;
auto headerColor = ImColor(color);
2022-02-01 22:09:44 +01:00
auto hoverColor = ImColor(color);
hoverColor.Value.w *= 1.3F;
ImGui::PushID(id);
ImGui::PushStyleColor(ImGuiCol_Header, color);
ImGui::PushStyleColor(ImGuiCol_HeaderActive, color);
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, u32(hoverColor));
bool open = true;
if (ImGui::CollapsingHeader(name.c_str(), &open)) {
2021-12-07 22:47:41 +01:00
ImGui::TextUnformatted("hex.builtin.view.bookmarks.title.info"_lang);
ImGui::Separator();
ImGui::TextFormatted("hex.builtin.view.bookmarks.address"_lang, region.address, region.address + region.size - 1, region.size);
if (ImGui::BeginChild("hexData", ImVec2(0, ImGui::GetTextLineHeight() * 8), true)) {
size_t offset = region.address % 0x10;
2022-01-15 14:14:53 +01:00
for (u8 byte = 0; byte < 0x10; byte++) {
ImGui::TextFormattedDisabled("{0:02X}", byte);
ImGui::SameLine();
}
ImGui::NewLine();
// TODO: Clip this somehow
// First line
{
std::array<u8, 0x10> bytes = { 0 };
2022-02-01 22:09:44 +01:00
size_t byteCount = std::min<size_t>(0x10 - offset, region.size);
ImHexApi::Provider::get()->read(region.address, bytes.data() + offset, byteCount);
for (size_t byte = 0; byte < 0x10; byte++) {
if (byte < offset)
ImGui::TextUnformatted(" ");
else
ImGui::TextFormatted("{0:02X}", bytes[byte]);
ImGui::SameLine();
}
ImGui::NewLine();
}
// Other lines
{
std::array<u8, 0x10> bytes = { 0 };
for (u32 i = 0x10 - offset; i < region.size; i += 0x10) {
size_t byteCount = std::min<size_t>(region.size - i, 0x10);
ImHexApi::Provider::get()->read(region.address + i, bytes.data(), byteCount);
for (size_t byte = 0; byte < byteCount; byte++) {
ImGui::TextFormatted("{0:02X}", bytes[byte]);
ImGui::SameLine();
}
ImGui::NewLine();
}
}
}
ImGui::EndChild();
2021-12-07 22:47:41 +01:00
if (ImGui::Button("hex.builtin.view.bookmarks.button.jump"_lang))
2022-02-08 18:38:54 +01:00
ImHexApi::HexEditor::setSelection(region);
ImGui::SameLine(0, 15);
2021-02-25 21:51:12 +01:00
if (locked) {
if (ImGui::Button(ICON_FA_LOCK)) locked = false;
} else {
if (ImGui::Button(ICON_FA_UNLOCK)) locked = true;
}
ImGui::NewLine();
2021-12-07 22:47:41 +01:00
ImGui::TextUnformatted("hex.builtin.view.bookmarks.header.name"_lang);
ImGui::Separator();
ImGui::ColorEdit4("hex.builtin.view.bookmarks.header.color"_lang, (float *)&headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha | (locked ? ImGuiColorEditFlags_NoPicker : ImGuiColorEditFlags_None));
color = headerColor;
ImGui::SameLine();
if (locked)
ImGui::TextUnformatted(name.data());
else
ImGui::InputText("##nameInput", name);
ImGui::NewLine();
2021-12-07 22:47:41 +01:00
ImGui::TextUnformatted("hex.builtin.view.bookmarks.header.comment"_lang);
ImGui::Separator();
if (locked)
2022-01-15 14:14:53 +01:00
ImGui::TextFormattedWrapped("{}", comment.data());
else
ImGui::InputTextMultiline("##commentInput", comment);
ImGui::NewLine();
}
if (!open)
bookmarkToRemove = iter;
ImGui::PopID();
ImGui::PopStyleColor(3);
id++;
}
if (bookmarkToRemove != this->m_bookmarks.end()) {
ImHexApi::HexEditor::removeHighlight(bookmarkToRemove->highlightId);
this->m_bookmarks.erase(bookmarkToRemove);
2020-11-30 00:03:12 +01:00
ProjectFile::markDirty();
}
}
ImGui::EndChild();
}
ImGui::End();
}
}