diff options
author | Brendan Coles <bcoles@gmail.com> | 2021-05-22 07:44:10 +0000 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-05-22 13:13:49 +0430 |
commit | d1623350b5cbc5662ee6147e69d4b5e0b0cbbf6a (patch) | |
tree | 979a4fc0df5651c4a9ccf98620065e00275103cb | |
parent | ee5cf92b3dd62382e31de72a33996ce99336db5b (diff) | |
download | serenity-d1623350b5cbc5662ee6147e69d4b5e0b0cbbf6a.zip |
HexEditor: Add 'Select All' action
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.cpp | 17 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditor.h | 2 | ||||
-rw-r--r-- | Userland/Applications/HexEditor/HexEditorWidget.cpp | 7 |
3 files changed, 22 insertions, 4 deletions
diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index bf0dab3738..6304cbb8ee 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -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; } diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index d03d45f7f6..4406a3a849 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -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; diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index 76a9515c7a..ae124c6881 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -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; } })); |