HexEditor: Add option for showing/hiding the offsets column

This commit is contained in:
Sam Atkins 2024-04-07 12:01:37 +01:00 committed by Andreas Kling
parent 4640fc2c97
commit f6a9ea7265
4 changed files with 53 additions and 22 deletions

View file

@ -240,6 +240,15 @@ void HexEditor::update_content_size()
update();
}
void HexEditor::set_show_offsets_column(bool value)
{
if (value == m_show_offsets_column)
return;
m_show_offsets_column = value;
update_content_size();
}
void HexEditor::set_bytes_per_row(size_t bytes_per_row)
{
if (bytes_per_row == this->bytes_per_row())
@ -460,6 +469,8 @@ size_t HexEditor::group_width() const
int HexEditor::offset_area_width() const
{
if (!m_show_offsets_column)
return 0;
return m_padding + font().width_rounded_up("0X12345678"sv) + m_padding;
}
@ -677,14 +688,16 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
int text_area_start_x = hex_area_start_x + hex_area_width();
int text_area_text_start_x = text_area_start_x + m_padding;
Gfx::IntRect offset_clip_rect {
0,
vertical_scrollbar().value(),
offset_area_width(),
height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5
};
painter.fill_rect(offset_clip_rect, palette().ruler());
painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
if (m_show_offsets_column) {
Gfx::IntRect offset_clip_rect {
0,
vertical_scrollbar().value(),
offset_area_width(),
height() - height_occupied_by_horizontal_scrollbar()
};
painter.fill_rect(offset_clip_rect, palette().ruler());
painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
}
painter.draw_line({ text_area_start_x, 0 },
{ text_area_start_x, vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) },
@ -699,21 +712,23 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
int row_background_y = row_text_y - m_line_spacing / 2;
// Paint offsets
Gfx::IntRect side_offset_rect {
offset_area_text_start_x,
row_text_y,
width() - width_occupied_by_vertical_scrollbar(),
height() - height_occupied_by_horizontal_scrollbar()
};
if (m_show_offsets_column) {
Gfx::IntRect side_offset_rect {
offset_area_text_start_x,
row_text_y,
width() - width_occupied_by_vertical_scrollbar(),
height() - height_occupied_by_horizontal_scrollbar()
};
bool is_current_line = (m_position / bytes_per_row()) == row;
auto offset_text = String::formatted("{:#08X}", row * bytes_per_row()).release_value_but_fixme_should_propagate_errors();
painter.draw_text(
side_offset_rect,
offset_text,
is_current_line ? font().bold_variant() : font(),
Gfx::TextAlignment::TopLeft,
is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
bool is_current_line = (m_position / bytes_per_row()) == row;
auto offset_text = String::formatted("{:#08X}", row * bytes_per_row()).release_value_but_fixme_should_propagate_errors();
painter.draw_text(
side_offset_rect,
offset_text,
is_current_line ? font().bold_variant() : font(),
Gfx::TextAlignment::TopLeft,
is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
}
// Paint bytes
for (size_t column = 0; column < bytes_per_row(); column++) {

View file

@ -55,6 +55,8 @@ public:
bool copy_selected_hex_to_clipboard();
bool copy_selected_hex_to_clipboard_as_c_code();
void set_show_offsets_column(bool);
size_t bytes_per_group() const { return m_bytes_per_group; }
void set_bytes_per_group(size_t);
size_t groups_per_row() const { return m_groups_per_row; }
@ -95,6 +97,7 @@ private:
size_t m_content_length { 0 };
size_t m_bytes_per_group { 4 };
size_t m_groups_per_row { 4 };
bool m_show_offsets_column { true };
bool m_in_drag_select { false };
Selection m_selection;
size_t m_position { 0 };

View file

@ -326,6 +326,11 @@ ErrorOr<void> HexEditorWidget::setup()
Config::write_bool("HexEditor"sv, "Layout"sv, "ShowSearchResults"sv, action.is_checked());
});
m_show_offsets_column_action = GUI::Action::create_checkable("Show &Offsets Column", [&](auto& action) {
m_editor->set_show_offsets_column(action.is_checked());
Config::write_bool("HexEditor"sv, "Layout"sv, "ShowOffsetsColumn"sv, action.is_checked());
});
m_copy_hex_action = GUI::Action::create("Copy &Hex", { Mod_Ctrl, Key_C }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/hex.png"sv)), [&](const GUI::Action&) {
m_editor->copy_selected_hex_to_clipboard();
});
@ -625,6 +630,12 @@ ErrorOr<void> HexEditorWidget::initialize_menubar(GUI::Window& window)
view_menu->add_action(*m_layout_value_inspector_action);
view_menu->add_separator();
auto show_offsets_column = Config::read_bool("HexEditor"sv, "Layout"sv, "ShowOffsetsColumn"sv, true);
m_show_offsets_column_action->set_checked(show_offsets_column);
m_editor->set_show_offsets_column(show_offsets_column);
view_menu->add_action(*m_show_offsets_column_action);
view_menu->add_separator();
auto bytes_per_row = Config::read_i32("HexEditor"sv, "Layout"sv, "BytesPerRow"sv, 16);
m_editor->set_bytes_per_row(bytes_per_row);
m_editor->update();

View file

@ -87,6 +87,8 @@ private:
RefPtr<GUI::Action> m_copy_as_c_code_action;
RefPtr<GUI::Action> m_fill_selection_action;
RefPtr<GUI::Action> m_show_offsets_column_action;
GUI::ActionGroup m_bytes_per_row_actions;
GUI::ActionGroup m_value_inspector_mode_actions;