2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-09-13 23:48:22 +03:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
|
2022-02-10 22:28:48 +03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2022-03-03 17:20:47 +03:00
|
|
|
* Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-13 02:08:12 +03:00
|
|
|
#include "HexEditor.h"
|
2022-01-16 20:08:00 +03:00
|
|
|
#include "AK/Format.h"
|
2021-05-27 18:51:21 +03:00
|
|
|
#include "SearchResultsModel.h"
|
2021-01-24 17:28:26 +03:00
|
|
|
#include <AK/Debug.h>
|
2021-09-02 14:03:44 +03:00
|
|
|
#include <AK/ScopeGuard.h>
|
2019-10-13 02:08:12 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Clipboard.h>
|
|
|
|
#include <LibGUI/Menu.h>
|
2021-09-02 14:03:44 +03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
2021-04-13 17:18:20 +03:00
|
|
|
#include <LibGUI/Scrollbar.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/TextEditor.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2022-04-09 10:28:38 +03:00
|
|
|
#include <LibGfx/Font/FontDatabase.h>
|
2020-09-11 15:37:30 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-10-13 02:08:12 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2021-11-07 04:15:10 +03:00
|
|
|
#include <string.h>
|
2019-10-13 02:08:12 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-02-23 14:07:13 +03:00
|
|
|
HexEditor::HexEditor()
|
2021-09-25 00:30:23 +03:00
|
|
|
: m_blink_timer(Core::Timer::construct())
|
2022-01-20 20:47:39 +03:00
|
|
|
, m_document(make<HexDocumentMemory>(ByteBuffer::create_zeroed(0).release_value_but_fixme_should_propagate_errors()))
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-01-12 15:31:32 +03:00
|
|
|
set_should_hide_unnecessary_scrollbars(true);
|
2020-10-30 12:58:27 +03:00
|
|
|
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
2019-10-13 02:08:12 +03:00
|
|
|
set_scrollbars_enabled(true);
|
2020-12-29 20:25:13 +03:00
|
|
|
set_font(Gfx::FontDatabase::default_fixed_width_font());
|
2020-02-19 13:14:31 +03:00
|
|
|
set_background_role(ColorRole::Base);
|
|
|
|
set_foreground_role(ColorRole::BaseText);
|
2019-10-13 02:08:12 +03:00
|
|
|
vertical_scrollbar().set_step(line_height());
|
2021-09-25 00:30:23 +03:00
|
|
|
|
|
|
|
m_blink_timer->set_interval(500);
|
|
|
|
m_blink_timer->on_timeout = [this]() {
|
|
|
|
m_cursor_blink_active = !m_cursor_blink_active;
|
|
|
|
update();
|
|
|
|
};
|
|
|
|
m_blink_timer->start();
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-07-28 10:34:15 +03:00
|
|
|
ErrorOr<void> HexEditor::open_new_file(size_t size)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2022-07-28 10:34:15 +03:00
|
|
|
m_document = make<HexDocumentMemory>(TRY(ByteBuffer::create_zeroed(size)));
|
2021-11-10 03:37:10 +03:00
|
|
|
set_content_length(m_document->size());
|
|
|
|
m_position = 0;
|
|
|
|
m_cursor_at_low_nibble = false;
|
2021-11-10 04:09:35 +03:00
|
|
|
m_selection_start = 0;
|
|
|
|
m_selection_end = 0;
|
|
|
|
scroll_position_into_view(m_position);
|
2021-11-10 03:37:10 +03:00
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
|
2022-07-28 10:34:15 +03:00
|
|
|
return {};
|
2021-11-10 03:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void HexEditor::open_file(NonnullRefPtr<Core::File> file)
|
|
|
|
{
|
|
|
|
m_document = make<HexDocumentFile>(file);
|
|
|
|
set_content_length(m_document->size());
|
2020-02-19 10:56:57 +03:00
|
|
|
m_position = 0;
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
2021-11-10 04:09:35 +03:00
|
|
|
m_selection_start = 0;
|
|
|
|
m_selection_end = 0;
|
|
|
|
scroll_position_into_view(m_position);
|
2019-10-13 02:08:12 +03:00
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
}
|
|
|
|
|
2019-10-27 00:27:26 +03:00
|
|
|
void HexEditor::fill_selection(u8 fill_byte)
|
|
|
|
{
|
2019-10-27 01:41:59 +03:00
|
|
|
if (!has_selection())
|
2019-10-27 00:27:26 +03:00
|
|
|
return;
|
|
|
|
|
2022-03-13 20:40:19 +03:00
|
|
|
for (size_t i = m_selection_start; i < m_selection_end; i++)
|
2021-11-10 03:37:10 +03:00
|
|
|
m_document->set(i, fill_byte);
|
2019-10-27 00:27:26 +03:00
|
|
|
|
|
|
|
update();
|
|
|
|
did_change();
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
void HexEditor::set_position(size_t position)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-11-10 03:37:10 +03:00
|
|
|
if (position > m_document->size())
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_position = position;
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
2021-09-25 00:30:23 +03:00
|
|
|
reset_cursor_blink_state();
|
2019-10-13 02:08:12 +03:00
|
|
|
scroll_position_into_view(position);
|
|
|
|
update_status();
|
|
|
|
}
|
2022-03-03 17:20:47 +03:00
|
|
|
void HexEditor::set_selection(size_t position, size_t length)
|
|
|
|
{
|
|
|
|
if (position > m_document->size() || position + length > m_document->size())
|
|
|
|
return;
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2022-03-03 17:20:47 +03:00
|
|
|
m_position = position;
|
|
|
|
m_cursor_at_low_nibble = false;
|
|
|
|
m_selection_start = position;
|
|
|
|
m_selection_end = position + length;
|
|
|
|
reset_cursor_blink_state();
|
|
|
|
scroll_position_into_view(position);
|
|
|
|
update_status();
|
|
|
|
}
|
2022-01-16 11:41:34 +03:00
|
|
|
bool HexEditor::save_as(NonnullRefPtr<Core::File> new_file)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->type() == HexDocument::Type::File) {
|
2022-07-28 10:49:00 +03:00
|
|
|
auto& file_document = static_cast<HexDocumentFile&>(*m_document);
|
|
|
|
if (!file_document.write_to_file(new_file))
|
2021-11-10 03:37:10 +03:00
|
|
|
return false;
|
2022-07-28 10:49:00 +03:00
|
|
|
file_document.set_file(new_file);
|
2021-11-10 03:37:10 +03:00
|
|
|
} else {
|
2022-07-28 10:49:00 +03:00
|
|
|
auto& memory_document = static_cast<HexDocumentMemory&>(*m_document);
|
|
|
|
if (!memory_document.write_to_file(new_file))
|
2021-11-10 03:37:10 +03:00
|
|
|
return false;
|
|
|
|
m_document = make<HexDocumentFile>(new_file);
|
|
|
|
}
|
2021-09-02 14:03:44 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
update();
|
2021-09-02 14:03:44 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
bool HexEditor::save()
|
|
|
|
{
|
|
|
|
if (m_document->type() != HexDocument::Type::File) {
|
2019-10-13 02:08:12 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
static_cast<HexDocumentFile*>(m_document.ptr())->write_to_file();
|
2019-10-13 02:08:12 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:48:59 +03:00
|
|
|
size_t HexEditor::selection_size()
|
|
|
|
{
|
|
|
|
if (!has_selection())
|
|
|
|
return 0;
|
2021-09-27 14:14:08 +03:00
|
|
|
return m_selection_end - m_selection_start;
|
2021-06-01 10:48:59 +03:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:08:12 +03:00
|
|
|
bool HexEditor::copy_selected_hex_to_clipboard()
|
|
|
|
{
|
2019-10-27 01:41:59 +03:00
|
|
|
if (!has_selection())
|
2019-10-13 02:08:12 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
StringBuilder output_string_builder;
|
2022-01-16 20:08:00 +03:00
|
|
|
for (size_t i = m_selection_start; i < m_selection_end; i++)
|
2021-11-10 03:37:10 +03:00
|
|
|
output_string_builder.appendff("{:02X} ", m_document->get(i).value);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2020-09-05 17:16:01 +03:00
|
|
|
GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
|
2019-10-13 02:08:12 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexEditor::copy_selected_text_to_clipboard()
|
|
|
|
{
|
2019-10-27 01:41:59 +03:00
|
|
|
if (!has_selection())
|
2019-10-13 02:08:12 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
StringBuilder output_string_builder;
|
2022-01-16 20:08:00 +03:00
|
|
|
for (size_t i = m_selection_start; i < m_selection_end; i++)
|
2021-11-10 03:37:10 +03:00
|
|
|
output_string_builder.append(isprint(m_document->get(i).value) ? m_document->get(i).value : '.');
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2020-09-05 17:16:01 +03:00
|
|
|
GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
|
2019-10-13 02:08:12 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-24 01:47:42 +03:00
|
|
|
bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
|
|
|
|
{
|
2019-10-27 01:41:59 +03:00
|
|
|
if (!has_selection())
|
2019-10-24 01:47:42 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
StringBuilder output_string_builder;
|
2022-01-16 20:08:00 +03:00
|
|
|
output_string_builder.appendff("unsigned char raw_data[{}] = {{\n", m_selection_end - m_selection_start);
|
2022-07-11 20:32:29 +03:00
|
|
|
output_string_builder.append(" "sv);
|
2022-01-16 20:08:00 +03:00
|
|
|
for (size_t i = m_selection_start, j = 1; i < m_selection_end; i++, j++) {
|
2021-11-10 03:37:10 +03:00
|
|
|
output_string_builder.appendff("{:#02X}", m_document->get(i).value);
|
2022-03-13 20:38:30 +03:00
|
|
|
if (i >= m_selection_end - 1)
|
|
|
|
continue;
|
|
|
|
if ((j % 12) == 0)
|
2022-07-11 20:32:29 +03:00
|
|
|
output_string_builder.append(",\n "sv);
|
2022-03-13 20:38:30 +03:00
|
|
|
else
|
2022-07-11 20:32:29 +03:00
|
|
|
output_string_builder.append(", "sv);
|
2019-10-24 01:47:42 +03:00
|
|
|
}
|
2022-07-11 20:32:29 +03:00
|
|
|
output_string_builder.append("\n};\n"sv);
|
2019-10-24 01:47:42 +03:00
|
|
|
|
2020-09-05 17:16:01 +03:00
|
|
|
GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
|
2019-10-24 01:47:42 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
void HexEditor::set_bytes_per_row(size_t bytes_per_row)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
|
|
|
m_bytes_per_row = bytes_per_row;
|
2021-09-27 14:31:19 +03:00
|
|
|
auto newWidth = offset_margin_width() + (m_bytes_per_row * cell_width()) + 2 * m_padding + (m_bytes_per_row * character_width()) + 4 * m_padding;
|
|
|
|
auto newHeight = total_rows() * line_height() + 2 * m_padding;
|
2021-09-27 14:14:08 +03:00
|
|
|
set_content_size({ static_cast<int>(newWidth), static_cast<int>(newHeight) });
|
2019-10-13 02:08:12 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
void HexEditor::set_content_length(size_t length)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
|
|
|
if (length == m_content_length)
|
|
|
|
return;
|
|
|
|
m_content_length = length;
|
2021-09-27 14:31:19 +03:00
|
|
|
auto newWidth = offset_margin_width() + (m_bytes_per_row * cell_width()) + 2 * m_padding + (m_bytes_per_row * character_width()) + 4 * m_padding;
|
|
|
|
auto newHeight = total_rows() * line_height() + 2 * m_padding;
|
2021-09-27 14:14:08 +03:00
|
|
|
set_content_size({ static_cast<int>(newWidth), static_cast<int>(newHeight) });
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-03-03 17:21:10 +03:00
|
|
|
Optional<u8> HexEditor::get_byte(size_t position)
|
|
|
|
{
|
|
|
|
if (position < m_document->size())
|
|
|
|
return m_document->get(position).value;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::mousedown_event(GUI::MouseEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-10-27 14:20:27 +03:00
|
|
|
if (event.button() != GUI::MouseButton::Primary) {
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto absolute_x = horizontal_scrollbar().value() + event.x();
|
|
|
|
auto absolute_y = vertical_scrollbar().value() + event.y();
|
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto hex_start_x = frame_thickness() + m_address_bar_width;
|
|
|
|
auto hex_start_y = frame_thickness() + m_padding;
|
|
|
|
auto hex_end_x = static_cast<int>(hex_start_x + bytes_per_row() * cell_width());
|
|
|
|
auto hex_end_y = static_cast<int>(hex_start_y + m_padding + total_rows() * line_height());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto text_start_x = static_cast<int>(frame_thickness() + m_address_bar_width + 2 * m_padding + bytes_per_row() * cell_width());
|
|
|
|
auto text_start_y = frame_thickness() + m_padding;
|
|
|
|
auto text_end_x = static_cast<int>(text_start_x + bytes_per_row() * character_width());
|
|
|
|
auto text_end_y = static_cast<int>(text_start_y + m_padding + total_rows() * line_height());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
|
|
|
if (absolute_x >= hex_start_x && absolute_x <= hex_end_x && absolute_y >= hex_start_y && absolute_y <= hex_end_y) {
|
2021-09-27 14:14:08 +03:00
|
|
|
if (absolute_x < hex_start_x || absolute_y < hex_start_y)
|
|
|
|
return;
|
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto byte_x = (absolute_x - hex_start_x) / cell_width();
|
2019-10-13 02:08:12 +03:00
|
|
|
auto byte_y = (absolute_y - hex_start_y) / line_height();
|
|
|
|
auto offset = (byte_y * m_bytes_per_row) + byte_x;
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
if (offset >= m_document->size())
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
2021-04-15 23:14:27 +03:00
|
|
|
dbgln_if(HEX_DEBUG, "HexEditor::mousedown_event(hex): offset={}", offset);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
|
|
|
m_edit_mode = EditMode::Hex;
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
2019-10-13 02:08:12 +03:00
|
|
|
m_position = offset;
|
|
|
|
m_in_drag_select = true;
|
|
|
|
m_selection_start = offset;
|
2020-12-27 11:46:08 +03:00
|
|
|
m_selection_end = offset;
|
2019-10-13 02:08:12 +03:00
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (absolute_x >= text_start_x && absolute_x <= text_end_x && absolute_y >= text_start_y && absolute_y <= text_end_y) {
|
2021-09-27 14:14:08 +03:00
|
|
|
if (absolute_x < hex_start_x || absolute_y < hex_start_y)
|
|
|
|
return;
|
|
|
|
|
2019-10-13 02:08:12 +03:00
|
|
|
auto byte_x = (absolute_x - text_start_x) / character_width();
|
|
|
|
auto byte_y = (absolute_y - text_start_y) / line_height();
|
|
|
|
auto offset = (byte_y * m_bytes_per_row) + byte_x;
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
if (offset >= m_document->size())
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
2021-04-15 23:14:27 +03:00
|
|
|
dbgln_if(HEX_DEBUG, "HexEditor::mousedown_event(text): offset={}", offset);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
|
|
|
m_position = offset;
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
2019-10-13 02:08:12 +03:00
|
|
|
m_in_drag_select = true;
|
|
|
|
m_selection_start = offset;
|
2020-12-27 11:46:08 +03:00
|
|
|
m_selection_end = offset;
|
2019-10-13 02:08:12 +03:00
|
|
|
m_edit_mode = EditMode::Text;
|
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::mousemove_event(GUI::MouseEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
|
|
|
auto absolute_x = horizontal_scrollbar().value() + event.x();
|
|
|
|
auto absolute_y = vertical_scrollbar().value() + event.y();
|
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto hex_start_x = frame_thickness() + m_address_bar_width;
|
|
|
|
auto hex_start_y = frame_thickness() + m_padding;
|
|
|
|
auto hex_end_x = static_cast<int>(hex_start_x + bytes_per_row() * cell_width());
|
|
|
|
auto hex_end_y = static_cast<int>(hex_start_y + m_padding + total_rows() * line_height());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto text_start_x = static_cast<int>(frame_thickness() + m_address_bar_width + 2 * m_padding + bytes_per_row() * cell_width());
|
|
|
|
auto text_start_y = frame_thickness() + m_padding;
|
|
|
|
auto text_end_x = static_cast<int>(text_start_x + bytes_per_row() * character_width());
|
|
|
|
auto text_end_y = static_cast<int>(text_start_y + m_padding + total_rows() * line_height());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
|
|
|
if ((absolute_x >= hex_start_x && absolute_x <= hex_end_x
|
|
|
|
&& absolute_y >= hex_start_y && absolute_y <= hex_end_y)
|
|
|
|
|| (absolute_x >= text_start_x && absolute_x <= text_end_x
|
|
|
|
&& absolute_y >= text_start_y && absolute_y <= text_end_y)) {
|
2020-09-11 15:37:30 +03:00
|
|
|
set_override_cursor(Gfx::StandardCursor::IBeam);
|
|
|
|
} else {
|
|
|
|
set_override_cursor(Gfx::StandardCursor::None);
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_in_drag_select) {
|
|
|
|
if (absolute_x >= hex_start_x && absolute_x <= hex_end_x && absolute_y >= hex_start_y && absolute_y <= hex_end_y) {
|
2021-09-27 14:14:08 +03:00
|
|
|
if (absolute_x < hex_start_x || absolute_y < hex_start_y)
|
|
|
|
return;
|
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto byte_x = (absolute_x - hex_start_x) / cell_width();
|
2019-10-13 02:08:12 +03:00
|
|
|
auto byte_y = (absolute_y - hex_start_y) / line_height();
|
|
|
|
auto offset = (byte_y * m_bytes_per_row) + byte_x;
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
if (offset > m_document->size())
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_selection_end = offset;
|
2021-09-25 00:30:23 +03:00
|
|
|
m_position = offset;
|
2019-10-13 02:08:12 +03:00
|
|
|
scroll_position_into_view(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (absolute_x >= text_start_x && absolute_x <= text_end_x && absolute_y >= text_start_y && absolute_y <= text_end_y) {
|
2021-09-27 14:14:08 +03:00
|
|
|
if (absolute_x < hex_start_x || absolute_y < hex_start_y)
|
|
|
|
return;
|
|
|
|
|
2019-10-13 02:08:12 +03:00
|
|
|
auto byte_x = (absolute_x - text_start_x) / character_width();
|
|
|
|
auto byte_y = (absolute_y - text_start_y) / line_height();
|
|
|
|
auto offset = (byte_y * m_bytes_per_row) + byte_x;
|
2021-11-10 03:37:10 +03:00
|
|
|
if (offset > m_document->size())
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_selection_end = offset;
|
2021-09-25 00:30:23 +03:00
|
|
|
m_position = offset;
|
2019-10-13 02:08:12 +03:00
|
|
|
scroll_position_into_view(offset);
|
|
|
|
}
|
|
|
|
update_status();
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::mouseup_event(GUI::MouseEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-10-27 14:20:27 +03:00
|
|
|
if (event.button() == GUI::MouseButton::Primary) {
|
2019-10-13 02:08:12 +03:00
|
|
|
if (m_in_drag_select) {
|
2020-12-27 11:46:08 +03:00
|
|
|
if (m_selection_end < m_selection_start) {
|
2019-10-21 07:51:00 +03:00
|
|
|
// lets flip these around
|
|
|
|
auto start = m_selection_end;
|
|
|
|
m_selection_end = m_selection_start;
|
|
|
|
m_selection_start = start;
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
m_in_drag_select = false;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
void HexEditor::scroll_position_into_view(size_t position)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-09-27 14:14:08 +03:00
|
|
|
size_t y = position / bytes_per_row();
|
|
|
|
size_t x = position % bytes_per_row();
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect rect {
|
2021-09-27 14:31:19 +03:00
|
|
|
static_cast<int>(frame_thickness() + offset_margin_width() + x * cell_width() + 2 * m_padding),
|
|
|
|
static_cast<int>(frame_thickness() + m_padding + y * line_height()),
|
|
|
|
static_cast<int>(cell_width()),
|
2021-09-27 14:14:08 +03:00
|
|
|
static_cast<int>(line_height() - m_line_spacing)
|
2019-10-13 02:08:12 +03:00
|
|
|
};
|
|
|
|
scroll_into_view(rect, true, true);
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::keydown_event(GUI::KeyEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-04-15 23:14:27 +03:00
|
|
|
dbgln_if(HEX_DEBUG, "HexEditor::keydown_event key={}", static_cast<u8>(event.key()));
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2022-03-13 21:08:10 +03:00
|
|
|
auto move_and_update_cursor_by = [&](i64 cursor_location_change) {
|
|
|
|
size_t new_position = m_position + cursor_location_change;
|
|
|
|
if (event.modifiers() & Mod_Shift) {
|
|
|
|
size_t selection_pivot = m_position == m_selection_end ? m_selection_start : m_selection_end;
|
|
|
|
m_position = new_position;
|
|
|
|
m_selection_start = selection_pivot;
|
|
|
|
m_selection_end = m_position;
|
|
|
|
if (m_selection_start > m_selection_end)
|
|
|
|
swap(m_selection_start, m_selection_end);
|
|
|
|
} else
|
|
|
|
m_selection_start = m_selection_end = m_position = new_position;
|
2022-02-19 17:52:09 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
|
|
|
reset_cursor_blink_state();
|
|
|
|
scroll_position_into_view(m_position);
|
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
};
|
|
|
|
|
2019-10-13 02:08:12 +03:00
|
|
|
if (event.key() == KeyCode::Key_Up) {
|
2022-03-13 21:08:10 +03:00
|
|
|
if (m_position >= bytes_per_row())
|
|
|
|
move_and_update_cursor_by(-bytes_per_row());
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key() == KeyCode::Key_Down) {
|
2022-03-13 21:08:10 +03:00
|
|
|
if (m_position + bytes_per_row() < m_document->size())
|
|
|
|
move_and_update_cursor_by(bytes_per_row());
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key() == KeyCode::Key_Left) {
|
2022-03-13 21:08:10 +03:00
|
|
|
if (m_position >= 1)
|
|
|
|
move_and_update_cursor_by(-1);
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key() == KeyCode::Key_Right) {
|
2022-03-13 21:08:10 +03:00
|
|
|
if (m_position + 1 < m_document->size())
|
|
|
|
move_and_update_cursor_by(1);
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key() == KeyCode::Key_Backspace) {
|
2022-03-13 21:08:10 +03:00
|
|
|
if (m_position > 0)
|
|
|
|
move_and_update_cursor_by(-1);
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-19 17:55:15 +03:00
|
|
|
if (event.key() == KeyCode::Key_PageUp) {
|
|
|
|
auto cursor_location_change = min(bytes_per_row() * visible_content_rect().height(), m_position);
|
2022-03-13 21:08:10 +03:00
|
|
|
if (cursor_location_change > 0)
|
|
|
|
move_and_update_cursor_by(-cursor_location_change);
|
2022-02-19 17:55:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key() == KeyCode::Key_PageDown) {
|
|
|
|
auto cursor_location_change = min(bytes_per_row() * visible_content_rect().height(), m_document->size() - m_position);
|
2022-03-13 21:08:10 +03:00
|
|
|
if (cursor_location_change > 0)
|
|
|
|
move_and_update_cursor_by(cursor_location_change);
|
2022-02-19 17:55:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-28 09:49:15 +03:00
|
|
|
if (!event.ctrl() && !event.alt() && !event.text().is_empty()) {
|
2019-10-13 02:08:12 +03:00
|
|
|
if (m_edit_mode == EditMode::Hex) {
|
|
|
|
hex_mode_keydown_event(event);
|
|
|
|
} else {
|
|
|
|
text_mode_keydown_event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
|
|
|
if ((event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) || (event.key() >= KeyCode::Key_A && event.key() <= KeyCode::Key_F)) {
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->size() == 0)
|
2021-09-25 00:30:23 +03:00
|
|
|
return;
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
VERIFY(m_position <= m_document->size());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
|
|
|
// yes, this is terrible... but it works.
|
|
|
|
auto value = (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9)
|
|
|
|
? event.key() - KeyCode::Key_0
|
|
|
|
: (event.key() - KeyCode::Key_A) + 0xA;
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
if (!m_cursor_at_low_nibble) {
|
2021-11-10 03:37:10 +03:00
|
|
|
u8 existing_change = m_document->get(m_position).value;
|
|
|
|
existing_change = value << 4 | (existing_change & 0xF); // shift new value left 4 bits, OR with existing last 4 bits
|
|
|
|
m_document->set(m_position, existing_change);
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = true;
|
2019-10-13 02:08:12 +03:00
|
|
|
} else {
|
2021-11-10 03:37:10 +03:00
|
|
|
m_document->set(m_position, (m_document->get(m_position).value & 0xF0) | value); // save the first 4 bits, OR the new value in the last 4
|
|
|
|
if (m_position + 1 < m_document->size())
|
2020-02-19 10:56:57 +03:00
|
|
|
m_position++;
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
|
2021-09-25 00:30:23 +03:00
|
|
|
reset_cursor_blink_state();
|
2019-10-13 02:08:12 +03:00
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
did_change();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::text_mode_keydown_event(GUI::KeyEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->size() == 0)
|
2020-02-19 10:56:57 +03:00
|
|
|
return;
|
2021-11-10 03:37:10 +03:00
|
|
|
VERIFY(m_position < m_document->size());
|
2020-02-19 10:56:57 +03:00
|
|
|
|
2020-12-29 18:35:02 +03:00
|
|
|
if (event.code_point() == 0) // This is a control key
|
|
|
|
return;
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
m_document->set(m_position, event.code_point());
|
|
|
|
if (m_position + 1 < m_document->size())
|
2020-02-19 10:56:57 +03:00
|
|
|
m_position++;
|
2021-09-27 14:14:08 +03:00
|
|
|
m_cursor_at_low_nibble = false;
|
2020-02-19 10:56:57 +03:00
|
|
|
|
2021-09-25 00:30:23 +03:00
|
|
|
reset_cursor_blink_state();
|
2019-10-13 02:08:12 +03:00
|
|
|
update();
|
|
|
|
update_status();
|
|
|
|
did_change();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexEditor::update_status()
|
|
|
|
{
|
|
|
|
if (on_status_change)
|
|
|
|
on_status_change(m_position, m_edit_mode, m_selection_start, m_selection_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexEditor::did_change()
|
|
|
|
{
|
|
|
|
if (on_change)
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void HexEditor::paint_event(GUI::PaintEvent& event)
|
2019-10-13 02:08:12 +03:00
|
|
|
{
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Frame::paint_event(event);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::Painter painter(*this);
|
2019-10-13 02:08:12 +03:00
|
|
|
painter.add_clip_rect(widget_inner_rect());
|
|
|
|
painter.add_clip_rect(event.rect());
|
2020-02-19 13:14:31 +03:00
|
|
|
painter.fill_rect(event.rect(), palette().color(background_role()));
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->size() == 0)
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
painter.translate(frame_thickness(), frame_thickness());
|
|
|
|
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect offset_clip_rect {
|
2019-10-13 02:08:12 +03:00
|
|
|
0,
|
|
|
|
vertical_scrollbar().value(),
|
2021-09-27 14:31:19 +03:00
|
|
|
m_address_bar_width - m_padding,
|
2019-10-13 02:08:12 +03:00
|
|
|
height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5
|
|
|
|
};
|
2020-02-19 13:14:31 +03:00
|
|
|
painter.fill_rect(offset_clip_rect, palette().ruler());
|
|
|
|
painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-09-27 14:31:19 +03:00
|
|
|
auto margin_and_hex_width = static_cast<int>(offset_margin_width() + m_bytes_per_row * cell_width() + 3 * m_padding);
|
2019-10-13 02:08:12 +03:00
|
|
|
painter.draw_line({ margin_and_hex_width, 0 },
|
|
|
|
{ margin_and_hex_width, vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) },
|
2020-02-19 13:14:31 +03:00
|
|
|
palette().ruler_border());
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
size_t view_height = (height() - height_occupied_by_horizontal_scrollbar());
|
|
|
|
size_t min_row = max(0, vertical_scrollbar().value() / line_height()); // if below 0 then use 0
|
|
|
|
size_t max_row = min(total_rows(), min_row + ceil_div(view_height, line_height())); // if above calculated rows, use calculated rows
|
2019-10-13 02:08:12 +03:00
|
|
|
|
|
|
|
// paint offsets
|
2021-09-27 14:14:08 +03:00
|
|
|
for (size_t i = min_row; i < max_row; i++) {
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect side_offset_rect {
|
2021-09-27 14:31:19 +03:00
|
|
|
frame_thickness() + m_padding,
|
|
|
|
static_cast<int>(frame_thickness() + m_padding + i * line_height()),
|
2019-10-13 02:08:12 +03:00
|
|
|
width() - width_occupied_by_vertical_scrollbar(),
|
|
|
|
height() - height_occupied_by_horizontal_scrollbar()
|
|
|
|
};
|
|
|
|
|
2020-02-19 13:14:31 +03:00
|
|
|
bool is_current_line = (m_position / bytes_per_row()) == i;
|
2020-10-06 14:32:00 +03:00
|
|
|
auto line = String::formatted("{:#08X}", i * bytes_per_row());
|
2020-02-19 13:14:31 +03:00
|
|
|
painter.draw_text(
|
|
|
|
side_offset_rect,
|
|
|
|
line,
|
2021-05-20 19:40:48 +03:00
|
|
|
is_current_line ? font().bold_variant() : font(),
|
2020-02-19 13:14:31 +03:00
|
|
|
Gfx::TextAlignment::TopLeft,
|
|
|
|
is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
for (size_t i = min_row; i < max_row; i++) {
|
|
|
|
for (size_t j = 0; j < bytes_per_row(); j++) {
|
2019-10-13 02:08:12 +03:00
|
|
|
auto byte_position = (i * bytes_per_row()) + j;
|
2021-11-10 03:37:10 +03:00
|
|
|
if (byte_position >= m_document->size())
|
2019-10-13 02:08:12 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool const edited_flag = m_document->get(byte_position).modified;
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool const selection_inbetween_start_end = byte_position >= m_selection_start && byte_position < m_selection_end;
|
|
|
|
bool const selection_inbetween_end_start = byte_position >= m_selection_end && byte_position < m_selection_start;
|
|
|
|
bool const highlight_flag = selection_inbetween_start_end || selection_inbetween_end_start;
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect hex_display_rect {
|
2022-10-13 12:28:28 +03:00
|
|
|
frame_thickness() + offset_margin_width() + j * cell_width() + 2 * m_padding,
|
|
|
|
frame_thickness() + m_padding + i * line_height(),
|
2021-09-27 14:31:19 +03:00
|
|
|
cell_width(),
|
2019-10-13 02:08:12 +03:00
|
|
|
line_height() - m_line_spacing
|
|
|
|
};
|
2022-10-13 12:28:28 +03:00
|
|
|
Gfx::IntRect background_rect {
|
|
|
|
frame_thickness() + offset_margin_width() + j * cell_width() + 1 * m_padding,
|
|
|
|
frame_thickness() + m_line_spacing / 2 + i * line_height(),
|
|
|
|
cell_width(),
|
|
|
|
line_height()
|
|
|
|
};
|
2021-09-25 00:30:23 +03:00
|
|
|
|
2022-07-11 00:53:23 +03:00
|
|
|
const u8 cell_value = m_document->get(byte_position).value;
|
|
|
|
auto line = String::formatted("{:02X}", cell_value);
|
|
|
|
|
2021-09-25 00:30:23 +03:00
|
|
|
Gfx::Color background_color = palette().color(background_role());
|
2022-07-11 00:53:23 +03:00
|
|
|
Gfx::Color text_color = [&]() -> Gfx::Color {
|
|
|
|
if (edited_flag)
|
|
|
|
return Color::Red;
|
|
|
|
if (cell_value == 0x00)
|
|
|
|
return palette().color(ColorRole::PlaceholderText);
|
|
|
|
return palette().color(foreground_role());
|
|
|
|
}();
|
2021-09-25 00:30:23 +03:00
|
|
|
|
2019-10-21 07:51:00 +03:00
|
|
|
if (highlight_flag) {
|
2021-09-26 21:34:50 +03:00
|
|
|
background_color = edited_flag ? palette().selection().inverted() : palette().selection();
|
|
|
|
text_color = edited_flag ? palette().selection_text().inverted() : palette().selection_text();
|
2021-09-25 00:30:23 +03:00
|
|
|
} else if (byte_position == m_position && m_edit_mode == EditMode::Text) {
|
|
|
|
background_color = palette().inactive_selection();
|
2020-02-19 13:14:31 +03:00
|
|
|
text_color = palette().inactive_selection_text();
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
2022-10-13 12:28:28 +03:00
|
|
|
painter.fill_rect(background_rect, background_color);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
painter.draw_text(hex_display_rect, line, Gfx::TextAlignment::TopLeft, text_color);
|
2019-10-13 02:08:12 +03:00
|
|
|
|
2021-09-25 00:30:23 +03:00
|
|
|
if (m_edit_mode == EditMode::Hex) {
|
|
|
|
if (byte_position == m_position && m_cursor_blink_active) {
|
|
|
|
Gfx::IntRect cursor_position_rect {
|
2021-09-27 14:14:08 +03:00
|
|
|
static_cast<int>(hex_display_rect.left() + m_cursor_at_low_nibble * character_width()),
|
|
|
|
hex_display_rect.top(),
|
|
|
|
2,
|
|
|
|
hex_display_rect.height()
|
2021-09-25 00:30:23 +03:00
|
|
|
};
|
|
|
|
painter.fill_rect(cursor_position_rect, palette().text_cursor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
Gfx::IntRect text_display_rect {
|
2022-10-13 12:28:28 +03:00
|
|
|
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + j * character_width() + 4 * m_padding,
|
|
|
|
frame_thickness() + m_padding + i * line_height(),
|
|
|
|
character_width(),
|
|
|
|
line_height() - m_line_spacing
|
|
|
|
};
|
|
|
|
Gfx::IntRect text_background_rect {
|
|
|
|
frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + j * character_width() + 4 * m_padding,
|
|
|
|
frame_thickness() + m_line_spacing / 2 + i * line_height(),
|
|
|
|
character_width(),
|
|
|
|
line_height()
|
2019-10-13 02:08:12 +03:00
|
|
|
};
|
2021-09-25 00:30:23 +03:00
|
|
|
|
|
|
|
background_color = palette().color(background_role());
|
2022-07-11 00:53:23 +03:00
|
|
|
text_color = [&]() -> Gfx::Color {
|
|
|
|
if (edited_flag)
|
|
|
|
return Color::Red;
|
|
|
|
if (cell_value == 0x00)
|
|
|
|
return palette().color(ColorRole::PlaceholderText);
|
|
|
|
return palette().color(foreground_role());
|
|
|
|
}();
|
2021-09-25 00:30:23 +03:00
|
|
|
|
2019-10-21 07:51:00 +03:00
|
|
|
if (highlight_flag) {
|
2021-09-26 21:34:50 +03:00
|
|
|
background_color = edited_flag ? palette().selection().inverted() : palette().selection();
|
|
|
|
text_color = edited_flag ? palette().selection_text().inverted() : palette().selection_text();
|
2021-09-25 00:30:23 +03:00
|
|
|
} else if (byte_position == m_position && m_edit_mode == EditMode::Hex) {
|
|
|
|
background_color = palette().inactive_selection();
|
|
|
|
text_color = palette().inactive_selection_text();
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
|
2022-10-13 12:28:28 +03:00
|
|
|
painter.fill_rect(text_background_rect, background_color);
|
2021-11-10 03:37:10 +03:00
|
|
|
painter.draw_text(text_display_rect, String::formatted("{:c}", isprint(cell_value) ? cell_value : '.'), Gfx::TextAlignment::TopLeft, text_color);
|
2021-09-25 00:30:23 +03:00
|
|
|
|
|
|
|
if (m_edit_mode == EditMode::Text) {
|
|
|
|
if (byte_position == m_position && m_cursor_blink_active) {
|
|
|
|
Gfx::IntRect cursor_position_rect {
|
|
|
|
text_display_rect.left(), text_display_rect.top(), 2, text_display_rect.height()
|
|
|
|
};
|
|
|
|
painter.fill_rect(cursor_position_rect, palette().text_cursor());
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 02:08:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 23:22:00 +03:00
|
|
|
|
2021-05-22 10:44:10 +03:00
|
|
|
void HexEditor::select_all()
|
|
|
|
{
|
2022-10-12 18:17:30 +03:00
|
|
|
highlight(0, m_document->size());
|
2021-05-22 10:44:10 +03:00
|
|
|
set_position(0);
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
void HexEditor::highlight(size_t start, size_t end)
|
2021-05-22 10:44:10 +03:00
|
|
|
{
|
|
|
|
m_selection_start = start;
|
|
|
|
m_selection_end = end;
|
|
|
|
set_position(start);
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
Optional<size_t> HexEditor::find_and_highlight(ByteBuffer& needle, size_t start)
|
2021-05-27 18:51:21 +03:00
|
|
|
{
|
|
|
|
auto end_of_match = find(needle, start);
|
2021-09-27 14:14:08 +03:00
|
|
|
if (end_of_match.has_value()) {
|
2022-10-12 18:17:30 +03:00
|
|
|
highlight(end_of_match.value() - needle.size(), end_of_match.value());
|
2021-09-27 14:14:08 +03:00
|
|
|
}
|
2021-05-27 18:51:21 +03:00
|
|
|
return end_of_match;
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
Optional<size_t> HexEditor::find(ByteBuffer& needle, size_t start)
|
2021-01-22 23:22:00 +03:00
|
|
|
{
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->size() == 0)
|
2021-09-27 14:14:08 +03:00
|
|
|
return {};
|
2021-01-22 23:22:00 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
for (size_t i = start; i < m_document->size(); i++) {
|
|
|
|
if (m_document->get(i).value == *needle.data()) {
|
|
|
|
bool found = true;
|
|
|
|
for (size_t j = 1; j < needle.size(); j++) {
|
|
|
|
if (m_document->get(i + j).value != needle.data()[j]) {
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
auto end_of_match = i + needle.size();
|
|
|
|
return end_of_match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 23:22:00 +03:00
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
return {};
|
2021-01-22 23:22:00 +03:00
|
|
|
}
|
2021-05-27 18:51:21 +03:00
|
|
|
|
2021-09-27 14:14:08 +03:00
|
|
|
Vector<Match> HexEditor::find_all(ByteBuffer& needle, size_t start)
|
2021-05-27 18:51:21 +03:00
|
|
|
{
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->size() == 0 || needle.size() == 0)
|
2021-05-27 18:51:21 +03:00
|
|
|
return {};
|
|
|
|
|
|
|
|
Vector<Match> matches;
|
|
|
|
|
|
|
|
size_t i = start;
|
2021-11-10 03:37:10 +03:00
|
|
|
for (; i < m_document->size(); i++) {
|
|
|
|
if (m_document->get(i).value == *needle.data()) {
|
|
|
|
bool found = true;
|
|
|
|
for (size_t j = 1; j < needle.size(); j++) {
|
|
|
|
if (m_document->get(i + j).value != needle.data()[j]) {
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
matches.append({ i, String::formatted("{}", StringView { needle }.to_string().characters()) });
|
|
|
|
i += needle.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 18:51:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (matches.is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto first_match = matches.at(0);
|
|
|
|
highlight(first_match.offset, first_match.offset + first_match.value.length());
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
2021-05-29 11:49:35 +03:00
|
|
|
|
|
|
|
Vector<Match> HexEditor::find_all_strings(size_t min_length)
|
|
|
|
{
|
2021-11-10 03:37:10 +03:00
|
|
|
if (m_document->size() == 0)
|
2021-05-29 11:49:35 +03:00
|
|
|
return {};
|
|
|
|
|
|
|
|
Vector<Match> matches;
|
|
|
|
|
2021-11-10 03:37:10 +03:00
|
|
|
bool found_string = false;
|
|
|
|
size_t offset = 0;
|
2021-05-29 11:49:35 +03:00
|
|
|
StringBuilder builder;
|
2021-11-10 03:37:10 +03:00
|
|
|
for (size_t i = 0; i < m_document->size(); i++) {
|
|
|
|
char c = m_document->get(i).value;
|
2021-05-29 11:49:35 +03:00
|
|
|
if (isprint(c)) {
|
2021-11-10 03:37:10 +03:00
|
|
|
if (!found_string) {
|
2021-05-29 11:49:35 +03:00
|
|
|
offset = i;
|
2021-11-10 03:37:10 +03:00
|
|
|
found_string = true;
|
|
|
|
}
|
2021-05-29 11:49:35 +03:00
|
|
|
builder.append(c);
|
|
|
|
} else {
|
2022-07-28 09:40:27 +03:00
|
|
|
if (builder.length() >= min_length)
|
2021-05-29 11:49:35 +03:00
|
|
|
matches.append({ offset, builder.to_string() });
|
|
|
|
builder.clear();
|
2021-11-10 03:37:10 +03:00
|
|
|
found_string = false;
|
2021-05-29 11:49:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matches.is_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto first_match = matches.at(0);
|
|
|
|
highlight(first_match.offset, first_match.offset + first_match.value.length());
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
2021-09-25 00:30:23 +03:00
|
|
|
|
|
|
|
void HexEditor::reset_cursor_blink_state()
|
|
|
|
{
|
|
|
|
m_cursor_blink_active = true;
|
|
|
|
m_blink_timer->restart();
|
|
|
|
}
|