mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:17:34 +00:00
HexEditor: Added a multi-label status bar, and a new copy option.
Make use of the new multi-label status bar, and added a menu option to allow you to copy generated C code from a selection to the clipboard.
This commit is contained in:
parent
a1c89c2734
commit
e3fbd2fe60
3 changed files with 34 additions and 7 deletions
|
@ -116,6 +116,29 @@ bool HexEditor::copy_selected_text_to_clipboard()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
|
||||||
|
{
|
||||||
|
if (m_selection_start == -1 || m_selection_end == -1 || (m_selection_end - m_selection_start) < 0 || m_buffer.is_empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
StringBuilder output_string_builder;
|
||||||
|
output_string_builder.appendf("unsigned char raw_data[%d] = {\n", (m_selection_end - m_selection_start) + 1);
|
||||||
|
output_string_builder.append(" ");
|
||||||
|
for (int i = m_selection_start, j = 1; i <= m_selection_end; i++, j++) {
|
||||||
|
output_string_builder.appendf("0x%02X", m_buffer.data()[i]);
|
||||||
|
if (i != m_selection_end)
|
||||||
|
output_string_builder.append(", ");
|
||||||
|
if ((j % 12) == 0) {
|
||||||
|
output_string_builder.append("\n");
|
||||||
|
output_string_builder.append(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output_string_builder.append("\n};\n");
|
||||||
|
|
||||||
|
GClipboard::the().set_data(output_string_builder.to_string());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void HexEditor::set_bytes_per_row(int bytes_per_row)
|
void HexEditor::set_bytes_per_row(int bytes_per_row)
|
||||||
{
|
{
|
||||||
m_bytes_per_row = bytes_per_row;
|
m_bytes_per_row = bytes_per_row;
|
||||||
|
|
|
@ -26,6 +26,7 @@ public:
|
||||||
|
|
||||||
bool copy_selected_text_to_clipboard();
|
bool copy_selected_text_to_clipboard();
|
||||||
bool copy_selected_hex_to_clipboard();
|
bool copy_selected_hex_to_clipboard();
|
||||||
|
bool copy_selected_hex_to_clipboard_as_c_code();
|
||||||
|
|
||||||
int bytes_per_row() const { return m_bytes_per_row; }
|
int bytes_per_row() const { return m_bytes_per_row; }
|
||||||
void set_bytes_per_row(int);
|
void set_bytes_per_row(int);
|
||||||
|
|
|
@ -26,12 +26,11 @@ HexEditorWidget::HexEditorWidget()
|
||||||
m_editor = HexEditor::construct(this);
|
m_editor = HexEditor::construct(this);
|
||||||
|
|
||||||
m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) {
|
m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) {
|
||||||
m_statusbar->set_text(String::format("Offset: %8X, Edit Mode: %s, Selection Start: %d, Selection End: %d, Selected Bytes: %d",
|
m_statusbar->set_text(0, String::format("Offset: %8X", position));
|
||||||
position,
|
m_statusbar->set_text(1, String::format("Edit Mode: %s", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
|
||||||
edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text",
|
m_statusbar->set_text(2, String::format("Selection Start: %d", selection_start));
|
||||||
selection_start,
|
m_statusbar->set_text(3, String::format("Selection End: %d", selection_end));
|
||||||
selection_end,
|
m_statusbar->set_text(4, String::format("Selected Bytes: %d", (selection_end - selection_start) + 1));
|
||||||
(selection_end - selection_start) + 1));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
m_editor->on_change = [this] {
|
m_editor->on_change = [this] {
|
||||||
|
@ -41,7 +40,7 @@ HexEditorWidget::HexEditorWidget()
|
||||||
update_title();
|
update_title();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_statusbar = GStatusBar::construct(this);
|
m_statusbar = GStatusBar::construct(5, this);
|
||||||
|
|
||||||
m_open_action = GCommonActions::make_open_action([this](auto&) {
|
m_open_action = GCommonActions::make_open_action([this](auto&) {
|
||||||
Optional<String> open_path = GFilePicker::get_open_filepath();
|
Optional<String> open_path = GFilePicker::get_open_filepath();
|
||||||
|
@ -131,6 +130,10 @@ HexEditorWidget::HexEditorWidget()
|
||||||
edit_menu->add_action(GAction::create("Copy Text", [&](const GAction&) {
|
edit_menu->add_action(GAction::create("Copy Text", [&](const GAction&) {
|
||||||
m_editor->copy_selected_text_to_clipboard();
|
m_editor->copy_selected_text_to_clipboard();
|
||||||
}));
|
}));
|
||||||
|
edit_menu->add_separator();
|
||||||
|
edit_menu->add_action(GAction::create("Copy As C Code", [&](const GAction&) {
|
||||||
|
m_editor->copy_selected_hex_to_clipboard_as_c_code();
|
||||||
|
}));
|
||||||
menubar->add_menu(move(edit_menu));
|
menubar->add_menu(move(edit_menu));
|
||||||
|
|
||||||
auto view_menu = make<GMenu>("View");
|
auto view_menu = make<GMenu>("View");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue