From c77fe5161c4a5686522945ae1d360f696de838f3 Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Sat, 26 Oct 2019 16:27:26 -0500 Subject: [PATCH] HexEditor: Added fill selection action. Added the ability to fill the current selection with a given byte. --- Applications/HexEditor/HexEditor.cpp | 14 ++++++++++++++ Applications/HexEditor/HexEditor.h | 1 + Applications/HexEditor/HexEditorWidget.cpp | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/Applications/HexEditor/HexEditor.cpp b/Applications/HexEditor/HexEditor.cpp index 3ea3cdc587..46d00c4da1 100644 --- a/Applications/HexEditor/HexEditor.cpp +++ b/Applications/HexEditor/HexEditor.cpp @@ -44,6 +44,20 @@ void HexEditor::set_buffer(const ByteBuffer& buffer) update_status(); } +void HexEditor::fill_selection(u8 fill_byte) +{ + if (m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty()) + return; + + for (int i = m_selection_start; i <= m_selection_end; i++) { + m_tracked_changes.set(i, m_buffer.data()[i]); + m_buffer.data()[i] = fill_byte; + } + + update(); + did_change(); +} + void HexEditor::set_position(int position) { if (position > m_buffer.size()) diff --git a/Applications/HexEditor/HexEditor.h b/Applications/HexEditor/HexEditor.h index 8ca6f0afd5..4a0b5629b3 100644 --- a/Applications/HexEditor/HexEditor.h +++ b/Applications/HexEditor/HexEditor.h @@ -22,6 +22,7 @@ public: void set_readonly(bool); void set_buffer(const ByteBuffer&); + void fill_selection(u8 fill_byte); bool write_to_file(const StringView& path); bool copy_selected_text_to_clipboard(); diff --git a/Applications/HexEditor/HexEditorWidget.cpp b/Applications/HexEditor/HexEditorWidget.cpp index c6d42519bb..4d8d6323c8 100644 --- a/Applications/HexEditor/HexEditorWidget.cpp +++ b/Applications/HexEditor/HexEditorWidget.cpp @@ -147,6 +147,14 @@ HexEditorWidget::HexEditorWidget() }); auto edit_menu = make("Edit"); + edit_menu->add_action(GAction::create("Fill selection...", [&](const GAction&) { + auto input_box = GInputBox::construct("Fill byte (hex):", "Fill Selection", this); + if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) { + auto fill_byte = strtol(input_box->text_value().characters(), nullptr, 16); + m_editor->fill_selection(fill_byte); + } + })); + edit_menu->add_separator(); edit_menu->add_action(*m_goto_decimal_offset_action); edit_menu->add_action(*m_goto_hex_offset_action); edit_menu->add_separator();