diff --git a/lib/external/imgui/include/imgui_memory_editor.h b/lib/external/imgui/include/imgui_memory_editor.h index 09d682f69..c5eca3e5c 100644 --- a/lib/external/imgui/include/imgui_memory_editor.h +++ b/lib/external/imgui/include/imgui_memory_editor.h @@ -232,7 +232,7 @@ struct MemoryEditor if (ImGui::Begin(title, p_open, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoNavInputs)) { if (DataPreviewAddr != DataPreviewAddrOld || DataPreviewAddrEnd != DataPreviewAddrEndOld) { - hex::Region selectionRegion = { std::min(DataPreviewAddr, DataPreviewAddrEnd) + base_display_addr, std::max(DataPreviewAddr, DataPreviewAddrEnd) - std::min(DataPreviewAddr, DataPreviewAddrEnd) }; + hex::Region selectionRegion = { std::min(DataPreviewAddr, DataPreviewAddrEnd) + base_display_addr, (std::max(DataPreviewAddr, DataPreviewAddrEnd) - std::min(DataPreviewAddr, DataPreviewAddrEnd)) + 1 }; hex::EventManager::post(selectionRegion); } @@ -335,7 +335,7 @@ struct MemoryEditor else if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_End)) && DataPreviewAddr < mem_size - 1) { DataPreviewAddr = data_preview_addr_next = mem_size - 1; if (!ImGui::GetIO().KeyShift) DataPreviewAddrEnd = DataPreviewAddr; } } } else if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) { - DataPreviewAddr = data_preview_addr_next = DataPreviewAddrEnd = (size_t)-1; + DataPreviewAddr = DataPreviewAddrOld = DataPreviewAddrEnd = DataPreviewAddrEndOld = data_preview_addr_next = (size_t)-1; HighlightMin = HighlightMax = (size_t)-1; hex::EventManager::post(hex::Region{ (size_t)-1, 0 }); diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index c8f7381f1..b3b1c0755 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -59,7 +59,7 @@ namespace hex { using EventList = std::list>; template - [[nodiscard]] static EventList::iterator subscribe(typename E::Callback function) { + static EventList::iterator subscribe(typename E::Callback function) { return s_events.insert(s_events.end(), std::make_pair(E::id, new E(function))); } diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 6a997a799..56bcbcd21 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -48,6 +48,10 @@ namespace hex { void removeHighlight(u32 id); std::map &getHighlights(); + Region getSelection(); + void setSelection(const Region ®ion); + void setSelection(u64 address, size_t size); + } namespace Bookmarks { diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 7b761e2a8..b76f0275d 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -54,6 +54,23 @@ namespace hex { return s_highlights; } + Region getSelection() { + static Region selectedRegion; + EventManager::subscribe([](const Region ®ion) { + selectedRegion = region; + }); + + return selectedRegion; + } + + void setSelection(const Region ®ion) { + EventManager::post(region); + } + + void setSelection(u64 address, size_t size) { + setSelection({ address, size }); + } + } diff --git a/plugins/builtin/source/content/views/view_bookmarks.cpp b/plugins/builtin/source/content/views/view_bookmarks.cpp index c9a879f11..0372c607d 100644 --- a/plugins/builtin/source/content/views/view_bookmarks.cpp +++ b/plugins/builtin/source/content/views/view_bookmarks.cpp @@ -124,7 +124,7 @@ namespace hex::plugin::builtin { ImGui::EndChild(); if (ImGui::Button("hex.builtin.view.bookmarks.button.jump"_lang)) - EventManager::post(region); + ImHexApi::HexEditor::setSelection(region); ImGui::SameLine(0, 15); if (ImGui::Button("hex.builtin.view.bookmarks.button.remove"_lang)) diff --git a/plugins/builtin/source/content/views/view_disassembler.cpp b/plugins/builtin/source/content/views/view_disassembler.cpp index 4eec6f322..0e29f1833 100644 --- a/plugins/builtin/source/content/views/view_disassembler.cpp +++ b/plugins/builtin/source/content/views/view_disassembler.cpp @@ -21,7 +21,7 @@ namespace hex::plugin::builtin { this->m_codeRegion[0] = this->m_codeRegion[1] = 0; } else { this->m_codeRegion[0] = region.address; - this->m_codeRegion[1] = region.address + region.size; + this->m_codeRegion[1] = region.address + region.size - 1; } } }); @@ -115,7 +115,7 @@ namespace hex::plugin::builtin { ImGui::Checkbox("hex.builtin.common.match_selection"_lang, &this->m_shouldMatchSelection); if (ImGui::IsItemEdited()) { // Force execution of Region Selection Event - EventManager::post(Region { 0, 0 }); + ImHexApi::HexEditor::setSelection(0, 0); } ImGui::NewLine(); @@ -356,7 +356,7 @@ namespace hex::plugin::builtin { ImGui::TableNextRow(); ImGui::TableNextColumn(); if (ImGui::Selectable(("##DisassemblyLine"s + std::to_string(i)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) { - EventManager::post(Region { instruction.offset, instruction.size }); + ImHexApi::HexEditor::setSelection(instruction.offset, instruction.size); } ImGui::SameLine(); ImGui::TextFormatted("0x{0:X}", instruction.address); diff --git a/plugins/builtin/source/content/views/view_hashes.cpp b/plugins/builtin/source/content/views/view_hashes.cpp index 7df62637a..c21f3796b 100644 --- a/plugins/builtin/source/content/views/view_hashes.cpp +++ b/plugins/builtin/source/content/views/view_hashes.cpp @@ -19,7 +19,7 @@ namespace hex::plugin::builtin { this->m_hashRegion[0] = this->m_hashRegion[1] = 0; } else { this->m_hashRegion[0] = region.address; - this->m_hashRegion[1] = region.size + 1; // WARNING: get size - 1 as region size + this->m_hashRegion[1] = region.size; } this->m_shouldInvalidate = true; } @@ -55,7 +55,7 @@ namespace hex::plugin::builtin { ImGui::Checkbox("hex.builtin.common.match_selection"_lang, &this->m_shouldMatchSelection); if (ImGui::IsItemEdited()) { // Force execution of Region Selection Event - EventManager::post(Region { 0, 0 }); + ImHexApi::HexEditor::setSelection(0, 0); this->m_shouldInvalidate = true; } diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index c34e0d4b0..8461c5803 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -726,7 +726,7 @@ namespace hex::plugin::builtin { if (ImGui::Button("hex.builtin.view.hex_editor.menu.file.goto"_lang) || runGoto) { provider->setCurrentPage(std::floor(double(newOffset - baseAddress) / hex::prv::Provider::PageSize)); - EventManager::post(Region { newOffset, 1 }); + ImHexApi::HexEditor::setSelection(newOffset, 1); } ImGui::EndTabBar(); @@ -775,7 +775,7 @@ namespace hex::plugin::builtin { this->pasteBytes(); if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.edit.select_all"_lang, "CTRL + A", false, providerValid)) - EventManager::post(Region { provider->getBaseAddress(), provider->getActualSize() }); + ImHexApi::HexEditor::setSelection(provider->getBaseAddress(), provider->getActualSize()); ImGui::Separator(); @@ -992,7 +992,7 @@ namespace hex::plugin::builtin { ShortcutManager::addShortcut(this, CTRL + Keys::A, [] { auto provider = ImHexApi::Provider::get(); - EventManager::post(Region { provider->getBaseAddress(), provider->getActualSize() }); + ImHexApi::HexEditor::setSelection(provider->getBaseAddress(), provider->getActualSize()); }); } diff --git a/plugins/builtin/source/content/views/view_information.cpp b/plugins/builtin/source/content/views/view_information.cpp index 77628655c..12675f186 100644 --- a/plugins/builtin/source/content/views/view_information.cpp +++ b/plugins/builtin/source/content/views/view_information.cpp @@ -209,7 +209,7 @@ namespace hex::plugin::builtin { if (ImPlot::DragLineX("Position", &this->m_entropyHandlePosition, false)) { u64 address = u64(this->m_entropyHandlePosition * this->m_blockSize) + provider->getBaseAddress(); address = std::min(address, provider->getBaseAddress() + provider->getSize() - 1); - EventManager::post(Region { address, 1 }); + ImHexApi::HexEditor::setSelection(address, 1); } ImPlot::EndPlot(); diff --git a/plugins/builtin/source/content/views/view_patches.cpp b/plugins/builtin/source/content/views/view_patches.cpp index c385c41b2..03ab2fa7e 100644 --- a/plugins/builtin/source/content/views/view_patches.cpp +++ b/plugins/builtin/source/content/views/view_patches.cpp @@ -60,7 +60,7 @@ namespace hex::plugin::builtin { ImGui::TableNextColumn(); if (ImGui::Selectable(("##patchLine" + std::to_string(index)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) { - EventManager::post(Region { address, 1 }); + ImHexApi::HexEditor::setSelection(address, 1); } if (ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) { ImGui::OpenPopup("PatchContextMenu"); diff --git a/plugins/builtin/source/content/views/view_strings.cpp b/plugins/builtin/source/content/views/view_strings.cpp index 50a8d30f4..9726f8bbd 100644 --- a/plugins/builtin/source/content/views/view_strings.cpp +++ b/plugins/builtin/source/content/views/view_strings.cpp @@ -202,7 +202,7 @@ namespace hex::plugin::builtin { ImGui::TableNextRow(); ImGui::TableNextColumn(); if (ImGui::Selectable(("##StringLine"s + std::to_string(i)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) { - EventManager::post(Region { foundString.offset, foundString.size }); + ImHexApi::HexEditor::setSelection(foundString.offset, foundString.size); } ImGui::PushID(i + 1); createStringContextMenu(foundString); diff --git a/plugins/builtin/source/content/views/view_yara.cpp b/plugins/builtin/source/content/views/view_yara.cpp index 71f6bdb2f..29f87b388 100644 --- a/plugins/builtin/source/content/views/view_yara.cpp +++ b/plugins/builtin/source/content/views/view_yara.cpp @@ -101,7 +101,7 @@ namespace hex::plugin::builtin { ImGui::TableNextColumn(); ImGui::PushID(i); if (ImGui::Selectable("match", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { - EventManager::post(Region { u64(address), size_t(size) }); + ImHexApi::HexEditor::setSelection(address, size); } ImGui::PopID(); ImGui::SameLine();