1
0
mirror of synced 2025-01-31 03:53:44 +01:00

ui: Streamline region selection in different views

Fixes #660
This commit is contained in:
WerWolv 2022-08-07 12:20:40 +02:00
parent edfac4ef60
commit f62ca307b0
13 changed files with 76 additions and 44 deletions

View File

@ -139,4 +139,5 @@ namespace ImGui {
void HideTooltip();
bool BitCheckbox(const char* label, bool* v);
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <hex/ui/view.hpp>
#include <ui/widgets.hpp>
#include <hex/helpers/disassembler.hpp>
@ -29,9 +30,9 @@ namespace hex::plugin::builtin {
private:
bool m_disassembling = false;
u64 m_baseAddress = 0;
u64 m_codeRegion[2] = { 0 };
bool m_shouldMatchSelection = false;
u64 m_baseAddress = 0;
ui::SelectedRegion m_range = ui::SelectedRegion::EntireData;
Region m_codeRegion = { 0, 0 };
Architecture m_architecture = Architecture::ARM;
cs_mode m_mode = cs_mode(0);

View File

@ -4,6 +4,7 @@
#include <imgui.h>
#include <hex/ui/view.hpp>
#include <ui/widgets.hpp>
#include <atomic>
#include <vector>
@ -31,7 +32,7 @@ namespace hex::plugin::builtin {
};
struct SearchSettings {
int range = 0;
ui::SelectedRegion range = ui::SelectedRegion::EntireData;
enum class Mode : int {
Strings,

View File

@ -0,0 +1,25 @@
#pragma once
#include <imgui.h>
#include <hex/ui/imgui_imhex_extensions.h>
namespace hex::plugin::builtin::ui {
enum class SelectedRegion : int {
EntireData,
Selection
};
inline void regionSelectionPicker(SelectedRegion *region, bool showHeader = true, bool firstEntry = false) {
if (showHeader)
ImGui::Header("hex.builtin.common.range"_lang, firstEntry);
if (ImGui::RadioButton("hex.builtin.common.range.entire_data"_lang, *region == SelectedRegion::EntireData))
*region = SelectedRegion::EntireData;
if (ImGui::RadioButton("hex.builtin.common.range.selection"_lang, *region == SelectedRegion::Selection))
*region = SelectedRegion::Selection;
}
}

View File

@ -11,17 +11,6 @@ using namespace std::literals::string_literals;
namespace hex::plugin::builtin {
ViewDisassembler::ViewDisassembler() : View("hex.builtin.view.disassembler.name") {
EventManager::subscribe<EventRegionSelected>(this, [this](Region region) {
if (this->m_shouldMatchSelection) {
if (region.address == size_t(-1)) {
this->m_codeRegion[0] = this->m_codeRegion[1] = 0;
} else {
this->m_codeRegion[0] = region.address;
this->m_codeRegion[1] = region.address + region.size - 1;
}
}
});
EventManager::subscribe<EventProviderDeleted>(this, [this](const auto*) {
this->m_disassembly.clear();
});
@ -49,14 +38,14 @@ namespace hex::plugin::builtin {
auto provider = ImHexApi::Provider::get();
std::vector<u8> buffer(2048, 0x00);
size_t size = (this->m_codeRegion[1] - this->m_codeRegion[0] + 1);
size_t size = this->m_codeRegion.getSize();
auto task = ImHexApi::Tasks::createTask("hex.builtin.view.disassembler.disassembling", size);
for (u64 address = 0; address < size; address += 2048) {
task.update(address);
size_t bufferSize = std::min(u64(2048), (this->m_codeRegion[1] - this->m_codeRegion[0] + 1) - address);
provider->read(this->m_codeRegion[0] + address, buffer.data(), bufferSize);
size_t bufferSize = std::min(u64(2048), (size - address));
provider->read(this->m_codeRegion.getStartAddress() + address, buffer.data(), bufferSize);
size_t instructionCount = cs_disasm(capstoneHandle, buffer.data(), bufferSize, this->m_baseAddress + address, 0, &instructions);
if (instructionCount == 0)
@ -69,7 +58,7 @@ namespace hex::plugin::builtin {
const auto &instr = instructions[i];
Disassembly disassembly = { };
disassembly.address = instr.address;
disassembly.offset = this->m_codeRegion[0] + address + usedBytes;
disassembly.offset = this->m_codeRegion.getStartAddress() + address + usedBytes;
disassembly.size = instr.size;
disassembly.mnemonic = instr.mnemonic;
disassembly.operators = instr.op_str;
@ -105,10 +94,24 @@ namespace hex::plugin::builtin {
ImGui::TextUnformatted("hex.builtin.view.disassembler.position"_lang);
ImGui::Separator();
ImGui::InputScalar("hex.builtin.view.disassembler.base"_lang, ImGuiDataType_U64, &this->m_baseAddress, nullptr, nullptr, "%08llX", ImGuiInputTextFlags_CharsHexadecimal);
ImGui::InputScalarN("hex.builtin.view.disassembler.region"_lang, ImGuiDataType_U64, this->m_codeRegion, 2, nullptr, nullptr, "%08llX", ImGuiInputTextFlags_CharsHexadecimal);
ImGui::InputHexadecimal("hex.builtin.view.disassembler.base"_lang, &this->m_baseAddress, ImGuiInputTextFlags_CharsHexadecimal);
ui::regionSelectionPicker(&this->m_range);
switch (this->m_range) {
case ui::SelectedRegion::Selection: {
auto region = ImHexApi::HexEditor::getSelection();
if (region.has_value()) {
this->m_codeRegion = region.value();
}
}
break;
case ui::SelectedRegion::EntireData: {
auto base = provider->getBaseAddress();
this->m_codeRegion = { base, base + provider->getActualSize() - 1 };
}
break;
}
ImGui::Checkbox("hex.builtin.common.match_selection"_lang, &this->m_shouldMatchSelection);
if (ImGui::IsItemEdited()) {
// Force execution of Region Selection Event
ImHexApi::HexEditor::setSelection(0, 0);

View File

@ -311,7 +311,7 @@ namespace hex::plugin::builtin {
void ViewFind::runSearch() {
Region searchRegion = [this]{
if (this->m_searchSettings.range == 0 || !ImHexApi::HexEditor::isSelectionValid()) {
if (this->m_searchSettings.range == ui::SelectedRegion::EntireData || !ImHexApi::HexEditor::isSelectionValid()) {
auto provider = ImHexApi::Provider::get();
return Region { provider->getBaseAddress(), provider->getActualSize() };
} else {
@ -410,9 +410,7 @@ namespace hex::plugin::builtin {
ImGui::BeginDisabled(this->m_searchRunning);
{
ImGui::Header("hex.builtin.view.find.range"_lang, true);
ImGui::RadioButton("hex.builtin.view.find.range.entire_data"_lang, &this->m_searchSettings.range, 0);
ImGui::RadioButton("hex.builtin.view.find.range.selection"_lang, &this->m_searchSettings.range, 1);
ui::regionSelectionPicker(&this->m_searchSettings.range);
ImGui::NewLine();

View File

@ -84,6 +84,9 @@ namespace hex::plugin::builtin {
{ "hex.builtin.common.filter", "Filter" },
{ "hex.builtin.common.value", "Wert" },
{ "hex.builtin.common.offset", "Offset" },
{ "hex.builtin.common.range", "Bereich" },
{ "hex.builtin.common.range.entire_data", "Gesammte Daten" },
{ "hex.builtin.common.range.selection", "Selektion" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },
@ -385,9 +388,6 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.find.name", "Finden" },
{ "hex.builtin.view.find.searching", "Suchen..." },
{ "hex.builtin.view.find.demangled", "Demangled" },
{ "hex.builtin.view.find.range", "Bereich" },
{ "hex.builtin.view.find.range.entire_data", "Gesammte Daten" },
{ "hex.builtin.view.find.range.selection", "Selektion" },
{ "hex.builtin.view.find.strings", "Strings" },
{ "hex.builtin.view.find.strings.min_length", "Minimallänge" },
{ "hex.builtin.view.find.strings.match_settings", "Sucheinstellungen" },

View File

@ -86,6 +86,9 @@ namespace hex::plugin::builtin {
{ "hex.builtin.common.value", "Value" },
{ "hex.builtin.common.type", "Type" },
{ "hex.builtin.common.offset", "Offset" },
{ "hex.builtin.common.range", "Range" },
{ "hex.builtin.common.range.entire_data", "Entire Data" },
{ "hex.builtin.common.range.selection", "Selection" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },
@ -389,9 +392,6 @@ namespace hex::plugin::builtin {
{ "hex.builtin.view.find.name", "Find" },
{ "hex.builtin.view.find.searching", "Searching..." },
{ "hex.builtin.view.find.demangled", "Demangled" },
{ "hex.builtin.view.find.range", "Range" },
{ "hex.builtin.view.find.range.entire_data", "Entire Data" },
{ "hex.builtin.view.find.range.selection", "Selection" },
{ "hex.builtin.view.find.strings", "Strings" },
{ "hex.builtin.view.find.strings.min_length", "Minimum length" },
{ "hex.builtin.view.find.strings.match_settings", "Match Settings" },

View File

@ -84,6 +84,9 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.common.filter", "Filter" },
//{ "hex.builtin.common.value", "Value" },
{ "hex.builtin.common.offset", "Offset" },
//{ "hex.builtin.common.range", "Range" },
//{ "hex.builtin.common.range.entire_data", "Entire Data" },
//{ "hex.builtin.common.range.selection", "Selection" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },
@ -388,9 +391,6 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.view.find.name", "Find" },
// { "hex.builtin.view.find.searching", "Searching..." },
// { "hex.builtin.view.find.demangled", "Demangled" },
// { "hex.builtin.view.find.range", "Range" },
// { "hex.builtin.view.find.range.entire_data", "Entire Data" },
// { "hex.builtin.view.find.range.selection", "Selection" },
// { "hex.builtin.view.find.strings", "Strings" },
// { "hex.builtin.view.find.strings.min_length", "Minimum length" },
// { "hex.builtin.view.find.strings.match_settings", "Match Settings" },

View File

@ -84,6 +84,9 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.common.filter", "Filter" },
//{ "hex.builtin.common.value", "Value" },
{ "hex.builtin.common.offset", "オフセット" },
//{ "hex.builtin.common.range", "Range" },
//{ "hex.builtin.common.range.entire_data", "Entire Data" },
//{ "hex.builtin.common.range.selection", "Selection" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },

View File

@ -84,6 +84,9 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.common.filter", "Filter" },
//{ "hex.builtin.common.value", "Value" },
{ "hex.builtin.common.offset", "Offset" },
//{ "hex.builtin.common.range", "Range" },
//{ "hex.builtin.common.range.entire_data", "Entire Data" },
//{ "hex.builtin.common.range.selection", "Selection" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },
@ -386,9 +389,6 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.view.find.name", "Find" },
// { "hex.builtin.view.find.searching", "Searching..." },
// { "hex.builtin.view.find.demangled", "Demangled" },
// { "hex.builtin.view.find.range", "Range" },
// { "hex.builtin.view.find.range.entire_data", "Entire Data" },
// { "hex.builtin.view.find.range.selection", "Selection" },
// { "hex.builtin.view.find.strings", "Strings" },
// { "hex.builtin.view.find.strings.min_length", "Minimum length" },
// { "hex.builtin.view.find.strings.match_settings", "Match Settings" },

View File

@ -84,6 +84,9 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.common.filter", "Filter" },
//{ "hex.builtin.common.value", "Value" },
{ "hex.builtin.common.offset", "偏移" },
//{ "hex.builtin.common.range", "Range" },
//{ "hex.builtin.common.range.entire_data", "Entire Data" },
//{ "hex.builtin.common.range.selection", "Selection" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },
@ -389,9 +392,6 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.view.find.name", "Find" },
// { "hex.builtin.view.find.searching", "Searching..." },
// { "hex.builtin.view.find.demangled", "Demangled" },
// { "hex.builtin.view.find.range", "Range" },
// { "hex.builtin.view.find.range.entire_data", "Entire Data" },
// { "hex.builtin.view.find.range.selection", "Selection" },
// { "hex.builtin.view.find.strings", "Strings" },
// { "hex.builtin.view.find.strings.min_length", "Minimum length" },
// { "hex.builtin.view.find.strings.match_settings", "Match Settings" },

View File

@ -84,6 +84,9 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.common.filter", "Filter" },
//{ "hex.builtin.common.value", "Value" },
{ "hex.builtin.common.offset", "位移" },
//{ "hex.builtin.common.range", "Range" },
//{ "hex.builtin.common.range.entire_data", "Entire Data" },
//{ "hex.builtin.common.range.selection", "Selection" },
{ "hex.builtin.common.encoding.ascii", "ASCII" },
{ "hex.builtin.common.encoding.utf16le", "UTF-16LE" },
@ -387,9 +390,6 @@ namespace hex::plugin::builtin {
//{ "hex.builtin.view.find.name", "Find" },
// { "hex.builtin.view.find.searching", "Searching..." },
// { "hex.builtin.view.find.demangled", "Demangled" },
// { "hex.builtin.view.find.range", "Range" },
// { "hex.builtin.view.find.range.entire_data", "Entire Data" },
// { "hex.builtin.view.find.range.selection", "Selection" },
// { "hex.builtin.view.find.strings", "Strings" },
// { "hex.builtin.view.find.strings.min_length", "Minimum length" },
// { "hex.builtin.view.find.strings.match_settings", "Match Settings" },