HexEditor: Add 'Select All' action

This commit is contained in:
Brendan Coles 2021-05-22 07:44:10 +00:00 committed by Ali Mohammad Pur
parent ee5cf92b3d
commit d1623350b5
Notes: sideshowbarker 2024-07-18 17:34:35 +09:00
3 changed files with 22 additions and 4 deletions

View File

@ -558,6 +558,19 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
}
}
void HexEditor::select_all()
{
highlight(0, m_buffer.size());
set_position(0);
}
void HexEditor::highlight(int start, int end)
{
m_selection_start = start;
m_selection_end = end;
set_position(start);
}
int HexEditor::find_and_highlight(ByteBuffer& needle, int start)
{
if (m_buffer.is_empty())
@ -571,9 +584,7 @@ int HexEditor::find_and_highlight(ByteBuffer& needle, int start)
dbgln("find_and_highlight: start={} raw_offset={} relative_offset={}", start, raw_offset, relative_offset);
auto end_of_match = relative_offset + needle.size();
set_position(relative_offset);
m_selection_start = relative_offset;
m_selection_end = end_of_match;
highlight(relative_offset, end_of_match);
return end_of_match;
}

View File

@ -33,6 +33,7 @@ public:
void fill_selection(u8 fill_byte);
bool write_to_file(const String& path);
void select_all();
bool has_selection() const { return !(m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()); }
bool copy_selected_text_to_clipboard();
bool copy_selected_hex_to_clipboard();
@ -42,6 +43,7 @@ public:
void set_bytes_per_row(int);
void set_position(int position);
void highlight(int start, int end);
int find_and_highlight(ByteBuffer& needle, int start = 0);
Function<void(int, EditMode, int, int)> on_status_change; // position, edit mode, selection start, selection end
Function<void()> on_change;

View File

@ -37,7 +37,7 @@ HexEditorWidget::HexEditorWidget()
m_statusbar->set_text(1, String::formatted("Edit Mode: {}", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
m_statusbar->set_text(2, String::formatted("Selection Start: {}", selection_start));
m_statusbar->set_text(3, String::formatted("Selection End: {}", selection_end));
m_statusbar->set_text(4, String::formatted("Selected Bytes: {}", abs(selection_end - selection_start) + 1));
m_statusbar->set_text(4, String::formatted("Selected Bytes: {}", abs(selection_end - selection_start)));
};
m_editor->on_change = [this] {
@ -147,6 +147,10 @@ void HexEditorWidget::initialize_menubar(GUI::Menubar& menubar)
});
auto& edit_menu = menubar.add_menu("&Edit");
edit_menu.add_action(GUI::CommonActions::make_select_all_action([this](auto&) {
m_editor->select_all();
m_editor->update();
}));
edit_menu.add_action(GUI::Action::create("Fill &Selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
String value;
if (GUI::InputBox::show(window(), value, "Fill byte (hex):", "Fill Selection") == GUI::InputBox::ExecOK && !value.is_empty()) {
@ -184,6 +188,7 @@ void HexEditorWidget::initialize_menubar(GUI::Menubar& menubar)
GUI::MessageBox::show(window(), String::formatted("Pattern \"{}\" not found in this file", m_search_text), "Not found", GUI::MessageBox::Type::Warning);
return;
}
m_editor->update();
m_last_found_index = result;
}
}));