From e5e00bec8f61d4cf8853ad92fe68e7b8a082484e Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Sat, 22 May 2021 14:08:05 +0000 Subject: HexEditor: Construct user interface from GML --- Userland/Applications/HexEditor/CMakeLists.txt | 3 + .../Applications/HexEditor/HexEditorWidget.cpp | 71 +++++++++++++--------- Userland/Applications/HexEditor/HexEditorWidget.h | 4 ++ .../Applications/HexEditor/HexEditorWindow.gml | 25 ++++++++ 4 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 Userland/Applications/HexEditor/HexEditorWindow.gml (limited to 'Userland/Applications') diff --git a/Userland/Applications/HexEditor/CMakeLists.txt b/Userland/Applications/HexEditor/CMakeLists.txt index fd8ed78cfd..8e20117420 100644 --- a/Userland/Applications/HexEditor/CMakeLists.txt +++ b/Userland/Applications/HexEditor/CMakeLists.txt @@ -1,8 +1,11 @@ +compile_gml(HexEditorWindow.gml HexEditorWindowGML.h hex_editor_window_gml) + set(SOURCES HexEditor.cpp HexEditorWidget.cpp FindDialog.cpp main.cpp + HexEditorWindowGML.h ) serenity_app(HexEditor ICON app-hex-editor) diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index ae124c6881..9c6c6057da 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -8,6 +8,7 @@ #include "FindDialog.h" #include #include +#include #include #include #include @@ -21,16 +22,19 @@ #include #include #include +#include #include #include +REGISTER_WIDGET(HexEditor, HexEditor); + HexEditorWidget::HexEditorWidget() { - set_fill_with_background_color(true); - set_layout(); - layout()->set_spacing(2); - - m_editor = add(); + load_from_gml(hex_editor_window_gml); + m_toolbar = *find_descendant_of_type_named("toolbar"); + m_toolbar_container = *find_descendant_of_type_named("toolbar_container"); + m_editor = *find_descendant_of_type_named("editor"); + m_statusbar = *find_descendant_of_type_named("statusbar"); m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) { m_statusbar->set_text(0, String::formatted("Offset: {:#08X}", position)); @@ -47,8 +51,6 @@ HexEditorWidget::HexEditorWidget() update_title(); }; - m_statusbar = add(5); - m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) { if (m_document_dirty) { if (GUI::MessageBox::show(window(), "Save changes to current file first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel) != GUI::Dialog::ExecResult::ExecOK) @@ -108,6 +110,37 @@ HexEditorWidget::HexEditorWidget() dbgln("Wrote document to {}", save_path.value()); }); + m_find_action = GUI::Action::create("&Find", { Mod_Ctrl, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [&](const GUI::Action&) { + auto old_buffer = m_search_buffer; + if (FindDialog::show(window(), m_search_text, m_search_buffer) == GUI::InputBox::ExecOK) { + + bool same_buffers = false; + if (old_buffer.size() == m_search_buffer.size()) { + if (memcmp(old_buffer.data(), m_search_buffer.data(), old_buffer.size()) == 0) + same_buffers = true; + } + + auto result = m_editor->find_and_highlight(m_search_buffer, same_buffers ? last_found_index() : 0); + + if (result == -1) { + 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; + } + }); + + m_layout_toolbar_action = GUI::Action::create_checkable("&Toolbar", [&](auto& action) { + m_toolbar_container->set_visible(action.is_checked()); + }); + + m_toolbar->add_action(*m_new_action); + m_toolbar->add_action(*m_open_action); + m_toolbar->add_action(*m_save_action); + m_toolbar->add_separator(); + m_toolbar->add_action(*m_find_action); + m_editor->set_focus(true); } @@ -172,27 +205,7 @@ void HexEditorWidget::initialize_menubar(GUI::Menubar& menubar) m_editor->copy_selected_hex_to_clipboard_as_c_code(); })); edit_menu.add_separator(); - edit_menu.add_action(GUI::Action::create("&Find", { Mod_Ctrl, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [&](const GUI::Action&) { - auto old_buffer = m_search_buffer; - if (FindDialog::show(window(), m_search_text, m_search_buffer) == GUI::InputBox::ExecOK) { - - bool same_buffers = false; - if (old_buffer.size() == m_search_buffer.size()) { - if (memcmp(old_buffer.data(), m_search_buffer.data(), old_buffer.size()) == 0) - same_buffers = true; - } - - auto result = m_editor->find_and_highlight(m_search_buffer, same_buffers ? last_found_index() : 0); - - if (result == -1) { - 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; - } - })); - + edit_menu.add_action(*m_find_action); edit_menu.add_action(GUI::Action::create("Find &Next", { Mod_None, Key_F3 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find-next.png"), [&](const GUI::Action&) { if (m_search_text.is_empty() || m_search_buffer.is_empty()) { GUI::MessageBox::show(window(), "Nothing to search for", "Not found", GUI::MessageBox::Type::Warning); @@ -209,6 +222,8 @@ void HexEditorWidget::initialize_menubar(GUI::Menubar& menubar) })); auto& view_menu = menubar.add_menu("&View"); + view_menu.add_action(*m_layout_toolbar_action); + m_layout_toolbar_action->set_checked(true); m_bytes_per_row_actions.set_exclusive(true); auto& bytes_per_row_menu = view_menu.add_submenu("Bytes per &Row"); for (int i = 8; i <= 32; i += 8) { diff --git a/Userland/Applications/HexEditor/HexEditorWidget.h b/Userland/Applications/HexEditor/HexEditorWidget.h index dfadf51007..24ebc3cddd 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.h +++ b/Userland/Applications/HexEditor/HexEditorWidget.h @@ -44,12 +44,16 @@ private: RefPtr m_open_action; RefPtr m_save_action; RefPtr m_save_as_action; + RefPtr m_find_action; RefPtr m_goto_decimal_offset_action; RefPtr m_goto_hex_offset_action; + RefPtr m_layout_toolbar_action; GUI::ActionGroup m_bytes_per_row_actions; RefPtr m_statusbar; + RefPtr m_toolbar; + RefPtr m_toolbar_container; bool m_document_dirty { false }; }; diff --git a/Userland/Applications/HexEditor/HexEditorWindow.gml b/Userland/Applications/HexEditor/HexEditorWindow.gml new file mode 100644 index 0000000000..6480759158 --- /dev/null +++ b/Userland/Applications/HexEditor/HexEditorWindow.gml @@ -0,0 +1,25 @@ +@GUI::Widget { + name: "main" + fill_with_background_color: true + + layout: @GUI::VerticalBoxLayout { + spacing: 2 + } + + @GUI::ToolbarContainer { + name: "toolbar_container" + + @GUI::Toolbar { + name: "toolbar" + } + } + + @HexEditor::HexEditor { + name: "editor" + } + + @GUI::Statusbar { + name: "statusbar" + label_count: 5 + } +} -- cgit v1.2.3