2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_bookmarks.hpp"
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-08-06 21:33:15 +02:00
|
|
|
#include <hex/api/achievement_manager.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2023-03-16 14:40:26 +01:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/providers/provider.hpp>
|
2024-01-28 22:14:59 +01:00
|
|
|
|
2023-03-16 14:40:26 +01:00
|
|
|
#include <content/providers/view_provider.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2024-01-28 22:14:59 +01:00
|
|
|
#include <fonts/codicons_font.h>
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2024-01-08 21:51:48 +01:00
|
|
|
ViewBookmarks::ViewBookmarks() : View::Window("hex.builtin.view.bookmarks.name", ICON_VS_BOOKMARK) {
|
2023-08-29 12:14:12 +02:00
|
|
|
|
|
|
|
// Handle bookmark add requests sent by the API
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestAddBookmark::subscribe(this, [this](Region region, std::string name, std::string comment, color_t color, u64 *id) {
|
2022-02-01 18:09:40 +01:00
|
|
|
if (name.empty()) {
|
|
|
|
name = hex::format("hex.builtin.view.bookmarks.default_title"_lang, region.address, region.address + region.size - 1);
|
2021-01-14 17:01:44 +01:00
|
|
|
}
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
if (color == 0x00)
|
|
|
|
color = ImGui::GetColorU32(ImGuiCol_Header);
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_currBookmarkId += 1;
|
|
|
|
u64 bookmarkId = m_currBookmarkId;
|
2023-11-25 12:43:48 +01:00
|
|
|
if (id != nullptr)
|
|
|
|
*id = bookmarkId;
|
|
|
|
|
2024-06-09 10:28:58 +02:00
|
|
|
auto bookmark = ImHexApi::Bookmarks::Entry {
|
2022-08-08 21:23:52 +02:00
|
|
|
region,
|
2022-02-01 18:09:40 +01:00
|
|
|
name,
|
|
|
|
std::move(comment),
|
|
|
|
color,
|
2024-03-29 15:40:15 +01:00
|
|
|
true,
|
2023-11-25 12:43:48 +01:00
|
|
|
bookmarkId
|
|
|
|
};
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_bookmarks->emplace_back(std::move(bookmark), TextEditor());
|
2021-01-13 14:11:23 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ImHexApi::Provider::markDirty();
|
2023-04-06 17:36:28 +02:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
EventBookmarkCreated::post(m_bookmarks->back().entry);
|
2023-12-08 10:29:44 +01:00
|
|
|
EventHighlightingChanged::post();
|
2020-11-30 00:03:12 +01:00
|
|
|
});
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestRemoveBookmark::subscribe([this](u64 id) {
|
2023-12-19 13:10:25 +01:00
|
|
|
std::erase_if(m_bookmarks.get(), [id](const auto &bookmark) {
|
2023-12-02 23:11:31 +01:00
|
|
|
return bookmark.entry.id == id;
|
2023-11-25 12:43:48 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw hex editor background highlights for bookmarks
|
2023-04-17 16:18:48 +02:00
|
|
|
ImHexApi::HexEditor::addBackgroundHighlightingProvider([this](u64 address, const u8* data, size_t size, bool) -> std::optional<color_t> {
|
2022-05-27 20:42:07 +02:00
|
|
|
hex::unused(data);
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Check all bookmarks for potential overlaps with the current address
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &bookmark : *m_bookmarks) {
|
2023-12-02 23:11:31 +01:00
|
|
|
if (Region { address, size }.isWithin(bookmark.entry.region))
|
|
|
|
return bookmark.entry.color;
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw hex editor tooltips for bookmarks
|
2023-04-17 16:18:48 +02:00
|
|
|
ImHexApi::HexEditor::addTooltipProvider([this](u64 address, const u8 *data, size_t size) {
|
2022-05-27 20:42:07 +02:00
|
|
|
hex::unused(data);
|
2023-08-29 12:14:12 +02:00
|
|
|
|
|
|
|
// Loop over all bookmarks
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &[bookmark, editor] : *m_bookmarks) {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Make sure the bookmark overlaps the currently hovered address
|
2022-05-27 20:42:07 +02:00
|
|
|
if (!Region { address, size }.isWithin(bookmark.region))
|
|
|
|
continue;
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw tooltip
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::BeginTooltip();
|
|
|
|
|
2022-07-17 13:12:28 +02:00
|
|
|
ImGui::PushID(&bookmark);
|
|
|
|
if (ImGui::BeginTable("##tooltips", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
{
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw bookmark header
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::ColorButton("##color", ImColor(bookmark.color));
|
|
|
|
ImGui::SameLine(0, 10);
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{} ", bookmark.name);
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw extra information table when holding down shift
|
2022-05-27 20:42:07 +02:00
|
|
|
if (ImGui::GetIO().KeyShift) {
|
|
|
|
ImGui::Indent();
|
2022-07-17 13:12:28 +02:00
|
|
|
if (ImGui::BeginTable("##extra_info", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) {
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw region
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGuiExt::TextFormatted("{}: ", "hex.ui.common.region"_lang.get());
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("[ 0x{:08X} - 0x{:08X} ] ", bookmark.region.getStartAddress(), bookmark.region.getEndAddress());
|
2022-05-27 20:42:07 +02:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw comment if it's not empty
|
2022-05-27 20:42:07 +02:00
|
|
|
if (!bookmark.comment.empty() && bookmark.comment[0] != '\x00') {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormatted("{}: ", "hex.builtin.view.bookmarks.header.comment"_lang.get());
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-03-16 13:35:29 +01:00
|
|
|
ImGui::PushTextWrapPos(ImGui::CalcTextSize("X").x * 40);
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::TextFormattedWrapped("{}", bookmark.comment);
|
2023-03-16 13:35:29 +01:00
|
|
|
ImGui::PopTextWrapPos();
|
2022-05-27 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::Unindent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_TableRowBg, bookmark.color);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_TableRowBgAlt, bookmark.color);
|
|
|
|
ImGui::EndTable();
|
|
|
|
ImGui::PopStyleColor(2);
|
|
|
|
}
|
2022-07-17 13:12:28 +02:00
|
|
|
ImGui::PopID();
|
2022-05-27 20:42:07 +02:00
|
|
|
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
});
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Handle saving / restoring of bookmarks in projects
|
2022-08-08 21:23:52 +02:00
|
|
|
ProjectFile::registerPerProviderHandler({
|
|
|
|
.basePath = "bookmarks.json",
|
2022-09-20 15:33:36 +02:00
|
|
|
.required = false,
|
2023-12-27 16:33:49 +01:00
|
|
|
.load = [this](prv::Provider *provider, const std::fs::path &basePath, const Tar &tar) -> bool {
|
2023-03-23 11:23:07 +01:00
|
|
|
auto fileContent = tar.readString(basePath);
|
2022-08-08 21:23:52 +02:00
|
|
|
if (fileContent.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto data = nlohmann::json::parse(fileContent.begin(), fileContent.end());
|
2023-12-19 13:10:25 +01:00
|
|
|
m_bookmarks.get(provider).clear();
|
2023-04-17 16:18:48 +02:00
|
|
|
return this->importBookmarks(provider, data);
|
2022-08-08 21:23:52 +02:00
|
|
|
},
|
2023-12-27 16:33:49 +01:00
|
|
|
.store = [this](prv::Provider *provider, const std::fs::path &basePath, const Tar &tar) -> bool {
|
2022-08-08 21:23:52 +02:00
|
|
|
nlohmann::json data;
|
|
|
|
|
2023-04-17 16:18:48 +02:00
|
|
|
bool result = this->exportBookmarks(provider, data);
|
2023-03-23 11:23:07 +01:00
|
|
|
tar.writeString(basePath, data.dump(4));
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
return result;
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
});
|
2022-08-14 14:45:32 +02:00
|
|
|
|
2023-11-22 08:26:31 +01:00
|
|
|
ContentRegistry::Reports::addReportProvider([this](prv::Provider *provider) -> std::string {
|
|
|
|
std::string result;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
const auto &bookmarks = m_bookmarks.get(provider);
|
2023-12-02 23:11:31 +01:00
|
|
|
if (bookmarks.empty())
|
2023-11-22 08:26:31 +01:00
|
|
|
return "";
|
|
|
|
|
|
|
|
result += "## Bookmarks\n\n";
|
|
|
|
|
2023-12-02 23:11:31 +01:00
|
|
|
for (const auto &[bookmark, editor] : bookmarks) {
|
2024-03-28 22:25:28 +01:00
|
|
|
result += hex::format("### <span style=\"background-color: #{:06X}80\">{} [0x{:04X} - 0x{:04X}]</span>\n\n", hex::changeEndianness(bookmark.color, std::endian::big) >> 8, bookmark.name, bookmark.region.getStartAddress(), bookmark.region.getEndAddress());
|
2023-11-22 08:26:31 +01:00
|
|
|
|
|
|
|
for (const auto &line : hex::splitString(bookmark.comment, "\n"))
|
|
|
|
result += hex::format("> {}\n", line);
|
|
|
|
result += "\n";
|
|
|
|
|
|
|
|
result += "```\n";
|
|
|
|
result += hex::generateHexView(bookmark.region.getStartAddress(), bookmark.region.getSize(), provider);
|
|
|
|
result += "\n```\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
this->registerMenuItems();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewBookmarks::~ViewBookmarks() {
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestAddBookmark::unsubscribe(this);
|
|
|
|
EventProviderDeleted::unsubscribe(this);
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-07 10:53:08 +02:00
|
|
|
static void drawColorPopup(ImColor &color) {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Generate color picker palette
|
2023-12-27 16:33:49 +01:00
|
|
|
const static auto Palette = [] {
|
2022-10-07 10:53:08 +02:00
|
|
|
constexpr static auto ColorCount = 36;
|
|
|
|
std::array<ImColor, ColorCount> result = { 0 };
|
|
|
|
|
|
|
|
u32 counter = 0;
|
|
|
|
for (auto &color : result) {
|
|
|
|
ImGui::ColorConvertHSVtoRGB(float(counter) / float(ColorCount - 1), 0.8F, 0.8F, color.Value.x, color.Value.y, color.Value.z);
|
2023-08-29 12:14:12 +02:00
|
|
|
color.Value.w = 0.7F;
|
2022-10-07 10:53:08 +02:00
|
|
|
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}();
|
|
|
|
|
2023-11-14 16:50:38 +01:00
|
|
|
bool colorChanged = false;
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw default color picker
|
2023-12-27 16:33:49 +01:00
|
|
|
if (ImGui::ColorPicker4("##picker", &color.Value.x, ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview))
|
2023-11-14 16:50:38 +01:00
|
|
|
colorChanged = true;
|
2022-10-07 10:53:08 +02:00
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Draw color palette
|
2022-10-07 10:53:08 +02:00
|
|
|
int id = 0;
|
|
|
|
for (const auto &paletteColor : Palette) {
|
|
|
|
ImGui::PushID(id);
|
|
|
|
if ((id % 9) != 0)
|
2023-08-29 12:14:12 +02:00
|
|
|
ImGui::SameLine(0.0F, ImGui::GetStyle().ItemSpacing.y);
|
2022-10-07 10:53:08 +02:00
|
|
|
|
|
|
|
constexpr static ImGuiColorEditFlags flags = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoDragDrop;
|
|
|
|
if (ImGui::ColorButton("##palette", paletteColor.Value, flags, ImVec2(20, 20))) {
|
|
|
|
color = paletteColor;
|
2023-11-14 16:50:38 +01:00
|
|
|
colorChanged = true;
|
2022-10-07 10:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
id++;
|
|
|
|
}
|
2023-11-14 16:50:38 +01:00
|
|
|
|
|
|
|
if (colorChanged)
|
2023-12-08 10:29:44 +01:00
|
|
|
EventHighlightingChanged::post();
|
2022-10-07 10:53:08 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 20:11:55 +02:00
|
|
|
void ViewBookmarks::drawDropTarget(std::list<Bookmark>::iterator it, float height) {
|
|
|
|
height = std::max(height, 1.0F);
|
|
|
|
|
|
|
|
if (it != m_bookmarks->begin()) {
|
|
|
|
ImGui::SetCursorPosY(ImGui::GetCursorPosY() - height);
|
|
|
|
} else {
|
|
|
|
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + height);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::InvisibleButton("##DropTarget", ImVec2(ImGui::GetContentRegionAvail().x, height * 2.0F));
|
|
|
|
const auto dropTarget = ImRect(ImGui::GetItemRectMin(), ImVec2(ImGui::GetItemRectMax().x, ImGui::GetItemRectMin().y + 2_scaled));
|
|
|
|
|
|
|
|
if (it == m_bookmarks->begin()) {
|
|
|
|
ImGui::SetCursorPosY(ImGui::GetCursorPosY() - height);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_DragDropTarget, 0x00);
|
|
|
|
if (ImGui::BeginDragDropTarget()) {
|
|
|
|
ImGui::GetWindowDrawList()->AddRectFilled(dropTarget.Min, dropTarget.Max, ImGui::GetColorU32(ImGuiCol_ButtonActive));
|
|
|
|
|
|
|
|
if (auto payload = ImGui::AcceptDragDropPayload("BOOKMARK_PAYLOAD"); payload != nullptr) {
|
|
|
|
// Receive the bookmark id from the payload
|
|
|
|
u64 droppedBookmarkId = *static_cast<const u64*>(payload->Data);
|
|
|
|
|
|
|
|
// Find the correct bookmark with that id
|
|
|
|
auto droppedIter = std::ranges::find_if(m_bookmarks->begin(), m_bookmarks->end(), [droppedBookmarkId](const auto &bookmarkItem) {
|
|
|
|
return bookmarkItem.entry.id == droppedBookmarkId;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Swap the two bookmarks
|
|
|
|
if (droppedIter != m_bookmarks->end()) {
|
|
|
|
m_bookmarks->splice(it, m_bookmarks, droppedIter);
|
|
|
|
|
|
|
|
EventHighlightingChanged::post();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndDragDropTarget();
|
|
|
|
}
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
}
|
2022-07-23 20:38:38 +02:00
|
|
|
|
2024-06-09 20:11:55 +02:00
|
|
|
void ViewBookmarks::drawContent() {
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw filter input
|
|
|
|
ImGui::PushItemWidth(-1);
|
2023-12-19 13:10:25 +01:00
|
|
|
ImGuiExt::InputTextIcon("##filter", ICON_VS_FILTER, m_currFilter);
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::PopItemWidth();
|
2024-06-09 20:59:06 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
if (ImGui::BeginChild("##bookmarks")) {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (m_bookmarks->empty()) {
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGuiExt::TextFormattedCentered("hex.builtin.view.bookmarks.no_bookmarks"_lang);
|
|
|
|
}
|
2021-01-08 19:34:29 +01:00
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
auto bookmarkToRemove = m_bookmarks->end();
|
2024-06-09 20:11:55 +02:00
|
|
|
const auto defaultItemSpacing = ImGui::GetStyle().ItemSpacing.y;
|
|
|
|
|
|
|
|
ImGui::Dummy({ ImGui::GetContentRegionAvail().x, 0 });
|
|
|
|
drawDropTarget(m_bookmarks->begin(), defaultItemSpacing);
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw all bookmarks
|
2023-12-19 13:10:25 +01:00
|
|
|
for (auto it = m_bookmarks->begin(); it != m_bookmarks->end(); ++it) {
|
2023-12-02 23:11:31 +01:00
|
|
|
auto &[bookmark, editor] = *it;
|
|
|
|
auto &[region, name, comment, color, locked, bookmarkId] = bookmark;
|
2023-11-21 13:47:50 +01:00
|
|
|
|
|
|
|
// Apply filter
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_currFilter.empty()) {
|
|
|
|
if (!name.contains(m_currFilter) && !comment.contains(m_currFilter))
|
2023-11-21 13:47:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-13 14:11:23 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
auto headerColor = ImColor(color);
|
|
|
|
auto hoverColor = ImColor(color);
|
|
|
|
hoverColor.Value.w *= 1.3F;
|
|
|
|
|
|
|
|
// Draw bookmark header in the same color as the bookmark was set to
|
2024-01-08 10:56:53 +01:00
|
|
|
ImGui::PushID(bookmarkId);
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Header, color);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_HeaderActive, color);
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, u32(hoverColor));
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
ImGui::PopStyleColor(3);
|
2024-01-08 10:56:53 +01:00
|
|
|
ImGui::PopID();
|
2023-11-21 13:47:50 +01:00
|
|
|
};
|
|
|
|
|
2024-03-29 15:40:15 +01:00
|
|
|
bool notDeleted = true;
|
|
|
|
|
2024-06-09 20:11:55 +02:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2());
|
2024-03-29 15:40:15 +01:00
|
|
|
auto expanded = ImGui::CollapsingHeader(hex::format("{}###bookmark", name).c_str(), ¬Deleted);
|
2024-06-09 20:11:55 +02:00
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
2024-03-29 15:40:15 +01:00
|
|
|
if (!expanded) {
|
2023-11-21 13:47:50 +01:00
|
|
|
// Handle dragging bookmarks up and down when they're collapsed
|
|
|
|
|
2024-03-29 15:40:15 +01:00
|
|
|
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceNoHoldToOpenOthers | ImGuiDragDropFlags_SourceAllowNullID)) {
|
2024-01-08 10:56:53 +01:00
|
|
|
// Set the payload to the bookmark id
|
|
|
|
ImGui::SetDragDropPayload("BOOKMARK_PAYLOAD", &bookmarkId, sizeof(bookmarkId));
|
|
|
|
|
|
|
|
// Draw drag and drop tooltip
|
|
|
|
ImGui::ColorButton("##color", headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha);
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGuiExt::TextFormatted("{}", name);
|
|
|
|
|
|
|
|
if (!comment.empty()) {
|
2024-06-09 21:01:22 +02:00
|
|
|
ImGui::Separator();
|
2024-01-08 10:56:53 +01:00
|
|
|
ImGui::PushTextWrapPos(300_scaled);
|
|
|
|
ImGuiExt::TextFormatted("{}", comment);
|
|
|
|
ImGui::PopTextWrapPos();
|
|
|
|
}
|
2023-11-21 13:47:50 +01:00
|
|
|
|
2024-01-08 10:56:53 +01:00
|
|
|
ImGui::EndDragDropSource();
|
2022-07-23 20:38:38 +02:00
|
|
|
}
|
2024-06-09 10:28:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto nextPos = ImGui::GetCursorPos();
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - 70_scaled);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Draw jump to region button
|
|
|
|
if (ImGuiExt::DimmedIconButton(ICON_VS_DEBUG_STEP_BACK, ImGui::GetStyleColorVec4(ImGuiCol_Text)))
|
|
|
|
ImHexApi::HexEditor::setSelection(region);
|
2024-07-27 14:09:52 +02:00
|
|
|
ImGui::SetItemTooltip("%s", "hex.builtin.view.bookmarks.tooltip.jump_to"_lang.get());
|
2024-06-09 10:28:58 +02:00
|
|
|
|
|
|
|
ImGui::SameLine(0, 1_scaled);
|
|
|
|
|
|
|
|
// Draw open in new view button
|
|
|
|
if (ImGuiExt::DimmedIconButton(ICON_VS_GO_TO_FILE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
|
2024-06-09 20:11:55 +02:00
|
|
|
auto provider = ImHexApi::Provider::get();
|
2024-06-09 10:51:59 +02:00
|
|
|
TaskManager::doLater([region, provider, name]{
|
2024-06-09 10:28:58 +02:00
|
|
|
auto newProvider = ImHexApi::Provider::createProvider("hex.builtin.provider.view", true);
|
|
|
|
if (auto *viewProvider = dynamic_cast<ViewProvider*>(newProvider); viewProvider != nullptr) {
|
|
|
|
viewProvider->setProvider(region.getStartAddress(), region.getSize(), provider);
|
2024-06-09 10:51:59 +02:00
|
|
|
viewProvider->setName(hex::format("'{}' View", name));
|
|
|
|
|
2024-06-09 10:28:58 +02:00
|
|
|
if (viewProvider->open()) {
|
|
|
|
EventProviderOpened::post(viewProvider);
|
|
|
|
AchievementManager::unlockAchievement("hex.builtin.achievement.hex_editor", "hex.builtin.achievement.hex_editor.open_new_view.name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-07-27 14:09:52 +02:00
|
|
|
ImGui::SetItemTooltip("%s", "hex.builtin.view.bookmarks.tooltip.open_in_view"_lang.get());
|
2024-06-09 10:28:58 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 20:11:55 +02:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2());
|
|
|
|
drawDropTarget(std::next(it), defaultItemSpacing);
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
2024-06-09 10:28:58 +02:00
|
|
|
ImGui::SetCursorPos(nextPos);
|
|
|
|
ImGui::Dummy({});
|
|
|
|
|
|
|
|
if (expanded) {
|
2023-11-21 13:47:50 +01:00
|
|
|
const auto rowHeight = ImGui::GetTextLineHeightWithSpacing() + 2 * ImGui::GetStyle().FramePadding.y;
|
|
|
|
if (ImGui::BeginTable("##bookmark_table", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) {
|
|
|
|
ImGui::TableSetupColumn("##name");
|
|
|
|
ImGui::TableSetupColumn("##spacing", ImGuiTableColumnFlags_WidthFixed, 20);
|
|
|
|
ImGui::TableSetupColumn("##value", ImGuiTableColumnFlags_WidthStretch);
|
|
|
|
|
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
// Draw bookmark name
|
|
|
|
ImGui::TextUnformatted("hex.builtin.view.bookmarks.header.name"_lang);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
// Draw lock/unlock button
|
2024-03-29 15:40:15 +01:00
|
|
|
ImGuiExt::DimmedIconToggle(ICON_VS_LOCK, ICON_VS_UNLOCK, &locked);
|
|
|
|
if (locked)
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGuiExt::InfoTooltip("hex.builtin.view.bookmarks.tooltip.unlock"_lang);
|
2024-03-29 15:40:15 +01:00
|
|
|
else
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGuiExt::InfoTooltip("hex.builtin.view.bookmarks.tooltip.lock"_lang);
|
2022-09-28 21:40:31 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::SameLine();
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw color button
|
|
|
|
if (ImGui::ColorButton("hex.builtin.view.bookmarks.header.color"_lang, headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha)) {
|
|
|
|
if (!locked)
|
|
|
|
ImGui::OpenPopup("hex.builtin.view.bookmarks.header.color"_lang);
|
|
|
|
}
|
|
|
|
ImGuiExt::InfoTooltip("hex.builtin.view.bookmarks.header.color"_lang);
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw color picker
|
|
|
|
if (ImGui::BeginPopup("hex.builtin.view.bookmarks.header.color"_lang)) {
|
|
|
|
drawColorPopup(headerColor);
|
|
|
|
color = headerColor;
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2021-08-28 01:47:26 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::SameLine();
|
2021-08-28 01:47:26 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw bookmark name if the bookmark is locked or an input text box if it's unlocked
|
2023-12-27 16:33:49 +01:00
|
|
|
if (locked) {
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TextUnformatted(name.data());
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
|
|
|
|
ImGui::InputText("##nameInput", name);
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
}
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.ui.common.address"_lang);
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
// Draw the address of the bookmark
|
2023-12-02 23:11:31 +01:00
|
|
|
u64 begin = region.getStartAddress();
|
|
|
|
u64 end = region.getEndAddress();
|
|
|
|
|
2023-12-17 23:22:39 +01:00
|
|
|
if (!locked) {
|
2024-03-11 14:00:52 +01:00
|
|
|
bool updated = false;
|
|
|
|
|
2023-12-17 23:22:39 +01:00
|
|
|
ImGui::PushItemWidth(100_scaled);
|
2024-03-11 14:00:52 +01:00
|
|
|
if (ImGuiExt::InputHexadecimal("##begin", &begin))
|
|
|
|
updated = true;
|
|
|
|
|
2023-12-17 23:22:39 +01:00
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
ImGui::TextUnformatted(" - ");
|
|
|
|
ImGui::SameLine(0, 0);
|
2024-03-11 14:00:52 +01:00
|
|
|
|
|
|
|
if (ImGuiExt::InputHexadecimal("##end", &end))
|
|
|
|
updated = true;
|
|
|
|
|
2023-12-17 23:22:39 +01:00
|
|
|
ImGui::PopItemWidth();
|
2023-12-02 23:11:31 +01:00
|
|
|
|
2024-06-09 10:54:09 +02:00
|
|
|
if (updated && end >= begin) {
|
2023-12-17 23:22:39 +01:00
|
|
|
region = Region(begin, end - begin + 1);
|
2024-03-11 14:00:52 +01:00
|
|
|
EventHighlightingChanged::post();
|
|
|
|
}
|
2023-12-17 23:22:39 +01:00
|
|
|
} else {
|
|
|
|
ImGuiExt::TextFormatted("0x{:02X} - 0x{:02X}", begin, end);
|
|
|
|
}
|
2021-08-28 01:47:26 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextRow(ImGuiTableRowFlags_None, rowHeight);
|
|
|
|
ImGui::TableNextColumn();
|
2020-11-28 00:33:26 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw size of the bookmark
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.ui.common.size"_lang);
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGuiExt::TextFormatted(hex::toByteString(region.size));
|
2021-02-24 21:42:18 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
2022-10-07 10:53:08 +02:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Draw comment if the bookmark is locked or an input text box if it's unlocked
|
2023-12-02 23:11:31 +01:00
|
|
|
editor.SetReadOnly(locked);
|
|
|
|
editor.SetShowLineNumbers(!locked);
|
|
|
|
editor.SetShowCursor(!locked);
|
|
|
|
editor.SetShowWhitespaces(false);
|
|
|
|
|
|
|
|
if (!locked || (locked && !comment.empty())) {
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGuiExt::Header("hex.builtin.view.bookmarks.header.comment"_lang);
|
2023-12-02 23:11:31 +01:00
|
|
|
editor.Render("##comment", ImVec2(ImGui::GetContentRegionAvail().x, 150_scaled), true);
|
|
|
|
|
|
|
|
if (editor.IsTextChanged())
|
|
|
|
comment = editor.GetText();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
2022-03-26 16:55:48 +01:00
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::NewLine();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-11-21 13:47:50 +01:00
|
|
|
// Mark a bookmark for removal when the user clicks the remove button
|
2024-03-29 15:40:15 +01:00
|
|
|
if (!notDeleted)
|
2023-12-02 23:11:31 +01:00
|
|
|
bookmarkToRemove = it;
|
2023-11-21 13:47:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the bookmark that was marked for removal
|
2023-12-19 13:10:25 +01:00
|
|
|
if (bookmarkToRemove != m_bookmarks->end()) {
|
|
|
|
m_bookmarks->erase(bookmarkToRemove);
|
2023-12-08 10:29:44 +01:00
|
|
|
EventHighlightingChanged::post();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-21 13:47:50 +01:00
|
|
|
ImGui::EndChild();
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
bool ViewBookmarks::importBookmarks(prv::Provider *provider, const nlohmann::json &json) {
|
|
|
|
if (!json.contains("bookmarks"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (const auto &bookmark : json["bookmarks"]) {
|
|
|
|
if (!bookmark.contains("name") || !bookmark.contains("comment") || !bookmark.contains("color") || !bookmark.contains("region") || !bookmark.contains("locked"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto ®ion = bookmark["region"];
|
|
|
|
if (!region.contains("address") || !region.contains("size"))
|
|
|
|
continue;
|
|
|
|
|
2023-12-02 23:11:31 +01:00
|
|
|
TextEditor editor;
|
|
|
|
editor.SetText(bookmark["comment"]);
|
2023-12-19 13:10:25 +01:00
|
|
|
m_bookmarks.get(provider).push_back({
|
2023-12-02 23:11:31 +01:00
|
|
|
{
|
|
|
|
.region = { region["address"], region["size"] },
|
|
|
|
.name = bookmark["name"],
|
|
|
|
.comment = bookmark["comment"],
|
|
|
|
.color = bookmark["color"],
|
|
|
|
.locked = bookmark["locked"],
|
2024-01-08 10:56:53 +01:00
|
|
|
.id = bookmark.contains("id") ? bookmark["id"].get<u64>() : m_currBookmarkId.get(provider)
|
2023-12-02 23:11:31 +01:00
|
|
|
},
|
|
|
|
editor
|
2022-08-14 14:45:32 +02:00
|
|
|
});
|
2023-11-25 12:43:48 +01:00
|
|
|
if (bookmark.contains("id"))
|
2024-01-08 10:56:53 +01:00
|
|
|
m_currBookmarkId.get(provider) = std::max<u64>(m_currBookmarkId.get(provider), bookmark["id"].get<i64>() + 1);
|
2023-11-25 12:43:48 +01:00
|
|
|
else
|
2024-01-08 10:56:53 +01:00
|
|
|
m_currBookmarkId.get(provider) += 1;
|
2022-08-14 14:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ViewBookmarks::exportBookmarks(prv::Provider *provider, nlohmann::json &json) {
|
|
|
|
json["bookmarks"] = nlohmann::json::array();
|
|
|
|
size_t index = 0;
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &[bookmark, editor] : m_bookmarks.get(provider)) {
|
2022-08-14 14:45:32 +02:00
|
|
|
json["bookmarks"][index] = {
|
2023-11-25 12:43:48 +01:00
|
|
|
{ "name", bookmark.name },
|
2023-12-02 23:11:31 +01:00
|
|
|
{ "comment", editor.GetText() },
|
2023-11-25 12:43:48 +01:00
|
|
|
{ "color", bookmark.color },
|
2022-08-14 14:45:32 +02:00
|
|
|
{ "region", {
|
2023-11-25 12:43:48 +01:00
|
|
|
{ "address", bookmark.region.address },
|
|
|
|
{ "size", bookmark.region.size }
|
2022-08-14 14:45:32 +02:00
|
|
|
}
|
|
|
|
},
|
2023-11-25 12:43:48 +01:00
|
|
|
{ "locked", bookmark.locked },
|
|
|
|
{ "id", bookmark.id }
|
2022-08-14 14:45:32 +02:00
|
|
|
};
|
2023-12-02 23:11:31 +01:00
|
|
|
|
2022-08-14 14:45:32 +02:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewBookmarks::registerMenuItems() {
|
2023-03-20 13:11:43 +01:00
|
|
|
/* Create bookmark */
|
2024-01-08 21:51:48 +01:00
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.menu.edit.bookmark.create" }, ICON_VS_BOOKMARK, 1900, CTRLCMD + Keys::B, [&] {
|
2023-06-11 21:41:11 +02:00
|
|
|
if (!ImHexApi::HexEditor::isSelectionValid())
|
|
|
|
return;
|
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
auto selection = ImHexApi::HexEditor::getSelection();
|
|
|
|
ImHexApi::Bookmarks::add(selection->getStartAddress(), selection->getSize(), {}, {});
|
|
|
|
}, []{ return ImHexApi::Provider::isValid() && ImHexApi::HexEditor::isSelectionValid(); });
|
2022-08-14 14:45:32 +02:00
|
|
|
|
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.file", "hex.builtin.menu.file.import" }, 3000);
|
2022-08-14 14:45:32 +02:00
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
/* Import bookmarks */
|
2024-01-08 21:51:48 +01:00
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.import", "hex.builtin.menu.file.import.bookmark" }, ICON_VS_BOOKMARK, 3050, Shortcut::None, [this]{
|
2023-04-17 16:18:48 +02:00
|
|
|
fs::openFileBrowser(fs::DialogMode::Open, { { "Bookmarks File", "hexbm"} }, [&, this](const std::fs::path &path) {
|
2023-03-20 13:11:43 +01:00
|
|
|
try {
|
2023-04-17 16:18:48 +02:00
|
|
|
this->importBookmarks(ImHexApi::Provider::get(), nlohmann::json::parse(wolv::io::File(path, wolv::io::File::Mode::Read).readString()));
|
2023-03-20 13:11:43 +01:00
|
|
|
} catch (...) { }
|
|
|
|
});
|
|
|
|
}, ImHexApi::Provider::isValid);
|
2022-08-14 14:45:32 +02:00
|
|
|
|
2023-03-20 13:11:43 +01:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.file", "hex.builtin.menu.file.export" }, 6200);
|
|
|
|
|
|
|
|
|
|
|
|
/* Export bookmarks */
|
2024-01-08 21:51:48 +01:00
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.export", "hex.builtin.menu.file.export.bookmark" }, ICON_VS_BOOKMARK, 6250, Shortcut::None, [this]{
|
2023-04-17 16:18:48 +02:00
|
|
|
fs::openFileBrowser(fs::DialogMode::Save, { { "Bookmarks File", "hexbm"} }, [&, this](const std::fs::path &path) {
|
2023-03-20 13:11:43 +01:00
|
|
|
nlohmann::json json;
|
2023-04-17 16:18:48 +02:00
|
|
|
this->exportBookmarks(ImHexApi::Provider::get(), json);
|
2023-03-20 13:11:43 +01:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
wolv::io::File(path, wolv::io::File::Mode::Create).writeString(json.dump(4));
|
2023-03-20 13:11:43 +01:00
|
|
|
});
|
2023-04-17 16:18:48 +02:00
|
|
|
}, [this]{
|
2023-12-19 13:10:25 +01:00
|
|
|
return ImHexApi::Provider::isValid() && !m_bookmarks->empty();
|
2022-08-14 14:45:32 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:33:26 +01:00
|
|
|
}
|