HexEditor.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "HexEditor.h"
  10. #include "AK/Format.h"
  11. #include "SearchResultsModel.h"
  12. #include <AK/Debug.h>
  13. #include <AK/ScopeGuard.h>
  14. #include <AK/StringBuilder.h>
  15. #include <LibGUI/Action.h>
  16. #include <LibGUI/Clipboard.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/Painter.h>
  20. #include <LibGUI/Scrollbar.h>
  21. #include <LibGUI/TextEditor.h>
  22. #include <LibGUI/Window.h>
  23. #include <LibGfx/Font/FontDatabase.h>
  24. #include <LibGfx/Palette.h>
  25. #include <ctype.h>
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. HexEditor::HexEditor()
  31. : m_blink_timer(Core::Timer::construct())
  32. , m_document(make<HexDocumentMemory>(ByteBuffer::create_zeroed(0).release_value_but_fixme_should_propagate_errors()))
  33. {
  34. set_should_hide_unnecessary_scrollbars(true);
  35. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  36. set_scrollbars_enabled(true);
  37. set_font(Gfx::FontDatabase::default_fixed_width_font());
  38. set_background_role(ColorRole::Base);
  39. set_foreground_role(ColorRole::BaseText);
  40. vertical_scrollbar().set_step(line_height());
  41. m_blink_timer->set_interval(500);
  42. m_blink_timer->on_timeout = [this]() {
  43. m_cursor_blink_active = !m_cursor_blink_active;
  44. update();
  45. };
  46. m_blink_timer->start();
  47. }
  48. ErrorOr<void> HexEditor::open_new_file(size_t size)
  49. {
  50. m_document = make<HexDocumentMemory>(TRY(ByteBuffer::create_zeroed(size)));
  51. set_content_length(m_document->size());
  52. m_position = 0;
  53. m_cursor_at_low_nibble = false;
  54. m_selection_start = 0;
  55. m_selection_end = 0;
  56. scroll_position_into_view(m_position);
  57. update();
  58. update_status();
  59. return {};
  60. }
  61. void HexEditor::open_file(NonnullRefPtr<Core::File> file)
  62. {
  63. m_document = make<HexDocumentFile>(file);
  64. set_content_length(m_document->size());
  65. m_position = 0;
  66. m_cursor_at_low_nibble = false;
  67. m_selection_start = 0;
  68. m_selection_end = 0;
  69. scroll_position_into_view(m_position);
  70. update();
  71. update_status();
  72. }
  73. void HexEditor::fill_selection(u8 fill_byte)
  74. {
  75. if (!has_selection())
  76. return;
  77. for (size_t i = m_selection_start; i < m_selection_end; i++)
  78. m_document->set(i, fill_byte);
  79. update();
  80. did_change();
  81. }
  82. void HexEditor::set_position(size_t position)
  83. {
  84. if (position > m_document->size())
  85. return;
  86. m_position = position;
  87. m_cursor_at_low_nibble = false;
  88. reset_cursor_blink_state();
  89. scroll_position_into_view(position);
  90. update_status();
  91. }
  92. void HexEditor::set_selection(size_t position, size_t length)
  93. {
  94. if (position > m_document->size() || position + length > m_document->size())
  95. return;
  96. m_position = position;
  97. m_cursor_at_low_nibble = false;
  98. m_selection_start = position;
  99. m_selection_end = position + length;
  100. reset_cursor_blink_state();
  101. scroll_position_into_view(position);
  102. update_status();
  103. }
  104. bool HexEditor::save_as(NonnullRefPtr<Core::File> new_file)
  105. {
  106. if (m_document->type() == HexDocument::Type::File) {
  107. HexDocumentFile* fileDocument = static_cast<HexDocumentFile*>(m_document.ptr());
  108. if (!fileDocument->write_to_file(new_file))
  109. return false;
  110. fileDocument->set_file(new_file);
  111. } else {
  112. HexDocumentMemory* memoryDocument = static_cast<HexDocumentMemory*>(m_document.ptr());
  113. if (!memoryDocument->write_to_file(new_file))
  114. return false;
  115. m_document = make<HexDocumentFile>(new_file);
  116. }
  117. update();
  118. return true;
  119. }
  120. bool HexEditor::save()
  121. {
  122. if (m_document->type() != HexDocument::Type::File) {
  123. return false;
  124. }
  125. static_cast<HexDocumentFile*>(m_document.ptr())->write_to_file();
  126. return true;
  127. }
  128. size_t HexEditor::selection_size()
  129. {
  130. if (!has_selection())
  131. return 0;
  132. return m_selection_end - m_selection_start;
  133. }
  134. bool HexEditor::copy_selected_hex_to_clipboard()
  135. {
  136. if (!has_selection())
  137. return false;
  138. StringBuilder output_string_builder;
  139. for (size_t i = m_selection_start; i < m_selection_end; i++)
  140. output_string_builder.appendff("{:02X} ", m_document->get(i).value);
  141. GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
  142. return true;
  143. }
  144. bool HexEditor::copy_selected_text_to_clipboard()
  145. {
  146. if (!has_selection())
  147. return false;
  148. StringBuilder output_string_builder;
  149. for (size_t i = m_selection_start; i < m_selection_end; i++)
  150. output_string_builder.append(isprint(m_document->get(i).value) ? m_document->get(i).value : '.');
  151. GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
  152. return true;
  153. }
  154. bool HexEditor::copy_selected_hex_to_clipboard_as_c_code()
  155. {
  156. if (!has_selection())
  157. return false;
  158. StringBuilder output_string_builder;
  159. output_string_builder.appendff("unsigned char raw_data[{}] = {{\n", m_selection_end - m_selection_start);
  160. output_string_builder.append(" "sv);
  161. for (size_t i = m_selection_start, j = 1; i < m_selection_end; i++, j++) {
  162. output_string_builder.appendff("{:#02X}", m_document->get(i).value);
  163. if (i >= m_selection_end - 1)
  164. continue;
  165. if ((j % 12) == 0)
  166. output_string_builder.append(",\n "sv);
  167. else
  168. output_string_builder.append(", "sv);
  169. }
  170. output_string_builder.append("\n};\n"sv);
  171. GUI::Clipboard::the().set_plain_text(output_string_builder.to_string());
  172. return true;
  173. }
  174. void HexEditor::set_bytes_per_row(size_t bytes_per_row)
  175. {
  176. m_bytes_per_row = bytes_per_row;
  177. auto newWidth = offset_margin_width() + (m_bytes_per_row * cell_width()) + 2 * m_padding + (m_bytes_per_row * character_width()) + 4 * m_padding;
  178. auto newHeight = total_rows() * line_height() + 2 * m_padding;
  179. set_content_size({ static_cast<int>(newWidth), static_cast<int>(newHeight) });
  180. update();
  181. }
  182. void HexEditor::set_content_length(size_t length)
  183. {
  184. if (length == m_content_length)
  185. return;
  186. m_content_length = length;
  187. auto newWidth = offset_margin_width() + (m_bytes_per_row * cell_width()) + 2 * m_padding + (m_bytes_per_row * character_width()) + 4 * m_padding;
  188. auto newHeight = total_rows() * line_height() + 2 * m_padding;
  189. set_content_size({ static_cast<int>(newWidth), static_cast<int>(newHeight) });
  190. }
  191. Optional<u8> HexEditor::get_byte(size_t position)
  192. {
  193. if (position < m_document->size())
  194. return m_document->get(position).value;
  195. return {};
  196. }
  197. void HexEditor::mousedown_event(GUI::MouseEvent& event)
  198. {
  199. if (event.button() != GUI::MouseButton::Primary) {
  200. return;
  201. }
  202. auto absolute_x = horizontal_scrollbar().value() + event.x();
  203. auto absolute_y = vertical_scrollbar().value() + event.y();
  204. auto hex_start_x = frame_thickness() + m_address_bar_width;
  205. auto hex_start_y = frame_thickness() + m_padding;
  206. auto hex_end_x = static_cast<int>(hex_start_x + bytes_per_row() * cell_width());
  207. auto hex_end_y = static_cast<int>(hex_start_y + m_padding + total_rows() * line_height());
  208. auto text_start_x = static_cast<int>(frame_thickness() + m_address_bar_width + 2 * m_padding + bytes_per_row() * cell_width());
  209. auto text_start_y = frame_thickness() + m_padding;
  210. auto text_end_x = static_cast<int>(text_start_x + bytes_per_row() * character_width());
  211. auto text_end_y = static_cast<int>(text_start_y + m_padding + total_rows() * line_height());
  212. if (absolute_x >= hex_start_x && absolute_x <= hex_end_x && absolute_y >= hex_start_y && absolute_y <= hex_end_y) {
  213. if (absolute_x < hex_start_x || absolute_y < hex_start_y)
  214. return;
  215. auto byte_x = (absolute_x - hex_start_x) / cell_width();
  216. auto byte_y = (absolute_y - hex_start_y) / line_height();
  217. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  218. if (offset >= m_document->size())
  219. return;
  220. dbgln_if(HEX_DEBUG, "HexEditor::mousedown_event(hex): offset={}", offset);
  221. m_edit_mode = EditMode::Hex;
  222. m_cursor_at_low_nibble = false;
  223. m_position = offset;
  224. m_in_drag_select = true;
  225. m_selection_start = offset;
  226. m_selection_end = offset;
  227. update();
  228. update_status();
  229. }
  230. if (absolute_x >= text_start_x && absolute_x <= text_end_x && absolute_y >= text_start_y && absolute_y <= text_end_y) {
  231. if (absolute_x < hex_start_x || absolute_y < hex_start_y)
  232. return;
  233. auto byte_x = (absolute_x - text_start_x) / character_width();
  234. auto byte_y = (absolute_y - text_start_y) / line_height();
  235. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  236. if (offset >= m_document->size())
  237. return;
  238. dbgln_if(HEX_DEBUG, "HexEditor::mousedown_event(text): offset={}", offset);
  239. m_position = offset;
  240. m_cursor_at_low_nibble = false;
  241. m_in_drag_select = true;
  242. m_selection_start = offset;
  243. m_selection_end = offset;
  244. m_edit_mode = EditMode::Text;
  245. update();
  246. update_status();
  247. }
  248. }
  249. void HexEditor::mousemove_event(GUI::MouseEvent& event)
  250. {
  251. auto absolute_x = horizontal_scrollbar().value() + event.x();
  252. auto absolute_y = vertical_scrollbar().value() + event.y();
  253. auto hex_start_x = frame_thickness() + m_address_bar_width;
  254. auto hex_start_y = frame_thickness() + m_padding;
  255. auto hex_end_x = static_cast<int>(hex_start_x + bytes_per_row() * cell_width());
  256. auto hex_end_y = static_cast<int>(hex_start_y + m_padding + total_rows() * line_height());
  257. auto text_start_x = static_cast<int>(frame_thickness() + m_address_bar_width + 2 * m_padding + bytes_per_row() * cell_width());
  258. auto text_start_y = frame_thickness() + m_padding;
  259. auto text_end_x = static_cast<int>(text_start_x + bytes_per_row() * character_width());
  260. auto text_end_y = static_cast<int>(text_start_y + m_padding + total_rows() * line_height());
  261. if ((absolute_x >= hex_start_x && absolute_x <= hex_end_x
  262. && absolute_y >= hex_start_y && absolute_y <= hex_end_y)
  263. || (absolute_x >= text_start_x && absolute_x <= text_end_x
  264. && absolute_y >= text_start_y && absolute_y <= text_end_y)) {
  265. set_override_cursor(Gfx::StandardCursor::IBeam);
  266. } else {
  267. set_override_cursor(Gfx::StandardCursor::None);
  268. }
  269. if (m_in_drag_select) {
  270. if (absolute_x >= hex_start_x && absolute_x <= hex_end_x && absolute_y >= hex_start_y && absolute_y <= hex_end_y) {
  271. if (absolute_x < hex_start_x || absolute_y < hex_start_y)
  272. return;
  273. auto byte_x = (absolute_x - hex_start_x) / cell_width();
  274. auto byte_y = (absolute_y - hex_start_y) / line_height();
  275. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  276. if (offset > m_document->size())
  277. return;
  278. m_selection_end = offset;
  279. m_position = offset;
  280. scroll_position_into_view(offset);
  281. }
  282. if (absolute_x >= text_start_x && absolute_x <= text_end_x && absolute_y >= text_start_y && absolute_y <= text_end_y) {
  283. if (absolute_x < hex_start_x || absolute_y < hex_start_y)
  284. return;
  285. auto byte_x = (absolute_x - text_start_x) / character_width();
  286. auto byte_y = (absolute_y - text_start_y) / line_height();
  287. auto offset = (byte_y * m_bytes_per_row) + byte_x;
  288. if (offset > m_document->size())
  289. return;
  290. m_selection_end = offset;
  291. m_position = offset;
  292. scroll_position_into_view(offset);
  293. }
  294. update_status();
  295. update();
  296. return;
  297. }
  298. }
  299. void HexEditor::mouseup_event(GUI::MouseEvent& event)
  300. {
  301. if (event.button() == GUI::MouseButton::Primary) {
  302. if (m_in_drag_select) {
  303. if (m_selection_end < m_selection_start) {
  304. // lets flip these around
  305. auto start = m_selection_end;
  306. m_selection_end = m_selection_start;
  307. m_selection_start = start;
  308. }
  309. m_in_drag_select = false;
  310. }
  311. update();
  312. update_status();
  313. }
  314. }
  315. void HexEditor::scroll_position_into_view(size_t position)
  316. {
  317. size_t y = position / bytes_per_row();
  318. size_t x = position % bytes_per_row();
  319. Gfx::IntRect rect {
  320. static_cast<int>(frame_thickness() + offset_margin_width() + x * cell_width() + 2 * m_padding),
  321. static_cast<int>(frame_thickness() + m_padding + y * line_height()),
  322. static_cast<int>(cell_width()),
  323. static_cast<int>(line_height() - m_line_spacing)
  324. };
  325. scroll_into_view(rect, true, true);
  326. }
  327. void HexEditor::keydown_event(GUI::KeyEvent& event)
  328. {
  329. dbgln_if(HEX_DEBUG, "HexEditor::keydown_event key={}", static_cast<u8>(event.key()));
  330. auto move_and_update_cursor_by = [&](i64 cursor_location_change) {
  331. size_t new_position = m_position + cursor_location_change;
  332. if (event.modifiers() & Mod_Shift) {
  333. size_t selection_pivot = m_position == m_selection_end ? m_selection_start : m_selection_end;
  334. m_position = new_position;
  335. m_selection_start = selection_pivot;
  336. m_selection_end = m_position;
  337. if (m_selection_start > m_selection_end)
  338. swap(m_selection_start, m_selection_end);
  339. } else
  340. m_selection_start = m_selection_end = m_position = new_position;
  341. m_cursor_at_low_nibble = false;
  342. reset_cursor_blink_state();
  343. scroll_position_into_view(m_position);
  344. update();
  345. update_status();
  346. };
  347. if (event.key() == KeyCode::Key_Up) {
  348. if (m_position >= bytes_per_row())
  349. move_and_update_cursor_by(-bytes_per_row());
  350. return;
  351. }
  352. if (event.key() == KeyCode::Key_Down) {
  353. if (m_position + bytes_per_row() < m_document->size())
  354. move_and_update_cursor_by(bytes_per_row());
  355. return;
  356. }
  357. if (event.key() == KeyCode::Key_Left) {
  358. if (m_position >= 1)
  359. move_and_update_cursor_by(-1);
  360. return;
  361. }
  362. if (event.key() == KeyCode::Key_Right) {
  363. if (m_position + 1 < m_document->size())
  364. move_and_update_cursor_by(1);
  365. return;
  366. }
  367. if (event.key() == KeyCode::Key_Backspace) {
  368. if (m_position > 0)
  369. move_and_update_cursor_by(-1);
  370. return;
  371. }
  372. if (event.key() == KeyCode::Key_PageUp) {
  373. auto cursor_location_change = min(bytes_per_row() * visible_content_rect().height(), m_position);
  374. if (cursor_location_change > 0)
  375. move_and_update_cursor_by(-cursor_location_change);
  376. return;
  377. }
  378. if (event.key() == KeyCode::Key_PageDown) {
  379. auto cursor_location_change = min(bytes_per_row() * visible_content_rect().height(), m_document->size() - m_position);
  380. if (cursor_location_change > 0)
  381. move_and_update_cursor_by(cursor_location_change);
  382. return;
  383. }
  384. if (!event.ctrl() && !event.alt() && !event.text().is_empty()) {
  385. if (m_edit_mode == EditMode::Hex) {
  386. hex_mode_keydown_event(event);
  387. } else {
  388. text_mode_keydown_event(event);
  389. }
  390. }
  391. }
  392. void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event)
  393. {
  394. if ((event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) || (event.key() >= KeyCode::Key_A && event.key() <= KeyCode::Key_F)) {
  395. if (m_document->size() == 0)
  396. return;
  397. VERIFY(m_position <= m_document->size());
  398. // yes, this is terrible... but it works.
  399. auto value = (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9)
  400. ? event.key() - KeyCode::Key_0
  401. : (event.key() - KeyCode::Key_A) + 0xA;
  402. if (!m_cursor_at_low_nibble) {
  403. u8 existing_change = m_document->get(m_position).value;
  404. existing_change = value << 4 | (existing_change & 0xF); // shift new value left 4 bits, OR with existing last 4 bits
  405. m_document->set(m_position, existing_change);
  406. m_cursor_at_low_nibble = true;
  407. } else {
  408. 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
  409. if (m_position + 1 < m_document->size())
  410. m_position++;
  411. m_cursor_at_low_nibble = false;
  412. }
  413. reset_cursor_blink_state();
  414. update();
  415. update_status();
  416. did_change();
  417. }
  418. }
  419. void HexEditor::text_mode_keydown_event(GUI::KeyEvent& event)
  420. {
  421. if (m_document->size() == 0)
  422. return;
  423. VERIFY(m_position < m_document->size());
  424. if (event.code_point() == 0) // This is a control key
  425. return;
  426. m_document->set(m_position, event.code_point());
  427. if (m_position + 1 < m_document->size())
  428. m_position++;
  429. m_cursor_at_low_nibble = false;
  430. reset_cursor_blink_state();
  431. update();
  432. update_status();
  433. did_change();
  434. }
  435. void HexEditor::update_status()
  436. {
  437. if (on_status_change)
  438. on_status_change(m_position, m_edit_mode, m_selection_start, m_selection_end);
  439. }
  440. void HexEditor::did_change()
  441. {
  442. if (on_change)
  443. on_change();
  444. }
  445. void HexEditor::paint_event(GUI::PaintEvent& event)
  446. {
  447. GUI::Frame::paint_event(event);
  448. GUI::Painter painter(*this);
  449. painter.add_clip_rect(widget_inner_rect());
  450. painter.add_clip_rect(event.rect());
  451. painter.fill_rect(event.rect(), palette().color(background_role()));
  452. if (m_document->size() == 0)
  453. return;
  454. painter.translate(frame_thickness(), frame_thickness());
  455. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  456. Gfx::IntRect offset_clip_rect {
  457. 0,
  458. vertical_scrollbar().value(),
  459. m_address_bar_width - m_padding,
  460. height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5
  461. };
  462. painter.fill_rect(offset_clip_rect, palette().ruler());
  463. painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
  464. auto margin_and_hex_width = static_cast<int>(offset_margin_width() + m_bytes_per_row * cell_width() + 3 * m_padding);
  465. painter.draw_line({ margin_and_hex_width, 0 },
  466. { margin_and_hex_width, vertical_scrollbar().value() + (height() - height_occupied_by_horizontal_scrollbar()) },
  467. palette().ruler_border());
  468. size_t view_height = (height() - height_occupied_by_horizontal_scrollbar());
  469. size_t min_row = max(0, vertical_scrollbar().value() / line_height()); // if below 0 then use 0
  470. size_t max_row = min(total_rows(), min_row + ceil_div(view_height, line_height())); // if above calculated rows, use calculated rows
  471. // paint offsets
  472. for (size_t i = min_row; i < max_row; i++) {
  473. Gfx::IntRect side_offset_rect {
  474. frame_thickness() + m_padding,
  475. static_cast<int>(frame_thickness() + m_padding + i * line_height()),
  476. width() - width_occupied_by_vertical_scrollbar(),
  477. height() - height_occupied_by_horizontal_scrollbar()
  478. };
  479. bool is_current_line = (m_position / bytes_per_row()) == i;
  480. auto line = String::formatted("{:#08X}", i * bytes_per_row());
  481. painter.draw_text(
  482. side_offset_rect,
  483. line,
  484. is_current_line ? font().bold_variant() : font(),
  485. Gfx::TextAlignment::TopLeft,
  486. is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
  487. }
  488. for (size_t i = min_row; i < max_row; i++) {
  489. for (size_t j = 0; j < bytes_per_row(); j++) {
  490. auto byte_position = (i * bytes_per_row()) + j;
  491. if (byte_position >= m_document->size())
  492. return;
  493. bool const edited_flag = m_document->get(byte_position).modified;
  494. bool const selection_inbetween_start_end = byte_position >= m_selection_start && byte_position < m_selection_end;
  495. bool const selection_inbetween_end_start = byte_position >= m_selection_end && byte_position < m_selection_start;
  496. bool const highlight_flag = selection_inbetween_start_end || selection_inbetween_end_start;
  497. Gfx::IntRect hex_display_rect {
  498. frame_thickness() + offset_margin_width() + static_cast<int>(j) * cell_width() + 2 * m_padding,
  499. frame_thickness() + m_padding + static_cast<int>(i) * line_height(),
  500. cell_width(),
  501. line_height() - m_line_spacing
  502. };
  503. const u8 cell_value = m_document->get(byte_position).value;
  504. auto line = String::formatted("{:02X}", cell_value);
  505. Gfx::Color background_color = palette().color(background_role());
  506. Gfx::Color text_color = [&]() -> Gfx::Color {
  507. if (edited_flag)
  508. return Color::Red;
  509. if (cell_value == 0x00)
  510. return palette().color(ColorRole::PlaceholderText);
  511. return palette().color(foreground_role());
  512. }();
  513. if (highlight_flag) {
  514. background_color = edited_flag ? palette().selection().inverted() : palette().selection();
  515. text_color = edited_flag ? palette().selection_text().inverted() : palette().selection_text();
  516. } else if (byte_position == m_position && m_edit_mode == EditMode::Text) {
  517. background_color = palette().inactive_selection();
  518. text_color = palette().inactive_selection_text();
  519. }
  520. painter.fill_rect(hex_display_rect, background_color);
  521. painter.draw_text(hex_display_rect, line, Gfx::TextAlignment::TopLeft, text_color);
  522. if (m_edit_mode == EditMode::Hex) {
  523. if (byte_position == m_position && m_cursor_blink_active) {
  524. Gfx::IntRect cursor_position_rect {
  525. static_cast<int>(hex_display_rect.left() + m_cursor_at_low_nibble * character_width()),
  526. hex_display_rect.top(),
  527. 2,
  528. hex_display_rect.height()
  529. };
  530. painter.fill_rect(cursor_position_rect, palette().text_cursor());
  531. }
  532. }
  533. Gfx::IntRect text_display_rect {
  534. static_cast<int>(frame_thickness() + offset_margin_width() + bytes_per_row() * cell_width() + j * character_width() + 4 * m_padding),
  535. static_cast<int>(frame_thickness() + m_padding + i * line_height()),
  536. static_cast<int>(character_width()),
  537. static_cast<int>(line_height() - m_line_spacing)
  538. };
  539. background_color = palette().color(background_role());
  540. text_color = [&]() -> Gfx::Color {
  541. if (edited_flag)
  542. return Color::Red;
  543. if (cell_value == 0x00)
  544. return palette().color(ColorRole::PlaceholderText);
  545. return palette().color(foreground_role());
  546. }();
  547. if (highlight_flag) {
  548. background_color = edited_flag ? palette().selection().inverted() : palette().selection();
  549. text_color = edited_flag ? palette().selection_text().inverted() : palette().selection_text();
  550. } else if (byte_position == m_position && m_edit_mode == EditMode::Hex) {
  551. background_color = palette().inactive_selection();
  552. text_color = palette().inactive_selection_text();
  553. }
  554. painter.fill_rect(text_display_rect, background_color);
  555. painter.draw_text(text_display_rect, String::formatted("{:c}", isprint(cell_value) ? cell_value : '.'), Gfx::TextAlignment::TopLeft, text_color);
  556. if (m_edit_mode == EditMode::Text) {
  557. if (byte_position == m_position && m_cursor_blink_active) {
  558. Gfx::IntRect cursor_position_rect {
  559. text_display_rect.left(), text_display_rect.top(), 2, text_display_rect.height()
  560. };
  561. painter.fill_rect(cursor_position_rect, palette().text_cursor());
  562. }
  563. }
  564. }
  565. }
  566. }
  567. void HexEditor::select_all()
  568. {
  569. highlight(0, m_document->size() - 1);
  570. set_position(0);
  571. }
  572. void HexEditor::highlight(size_t start, size_t end)
  573. {
  574. m_selection_start = start;
  575. m_selection_end = end;
  576. set_position(start);
  577. }
  578. Optional<size_t> HexEditor::find_and_highlight(ByteBuffer& needle, size_t start)
  579. {
  580. auto end_of_match = find(needle, start);
  581. if (end_of_match.has_value()) {
  582. highlight(end_of_match.value() - needle.size(), end_of_match.value() - 1);
  583. }
  584. return end_of_match;
  585. }
  586. Optional<size_t> HexEditor::find(ByteBuffer& needle, size_t start)
  587. {
  588. if (m_document->size() == 0)
  589. return {};
  590. for (size_t i = start; i < m_document->size(); i++) {
  591. if (m_document->get(i).value == *needle.data()) {
  592. bool found = true;
  593. for (size_t j = 1; j < needle.size(); j++) {
  594. if (m_document->get(i + j).value != needle.data()[j]) {
  595. found = false;
  596. break;
  597. }
  598. }
  599. if (found) {
  600. auto end_of_match = i + needle.size();
  601. return end_of_match;
  602. }
  603. }
  604. }
  605. return {};
  606. }
  607. Vector<Match> HexEditor::find_all(ByteBuffer& needle, size_t start)
  608. {
  609. if (m_document->size() == 0 || needle.size() == 0)
  610. return {};
  611. Vector<Match> matches;
  612. size_t i = start;
  613. for (; i < m_document->size(); i++) {
  614. if (m_document->get(i).value == *needle.data()) {
  615. bool found = true;
  616. for (size_t j = 1; j < needle.size(); j++) {
  617. if (m_document->get(i + j).value != needle.data()[j]) {
  618. found = false;
  619. break;
  620. }
  621. }
  622. if (found) {
  623. matches.append({ i, String::formatted("{}", StringView { needle }.to_string().characters()) });
  624. i += needle.size() - 1;
  625. }
  626. }
  627. }
  628. if (matches.is_empty())
  629. return {};
  630. auto first_match = matches.at(0);
  631. highlight(first_match.offset, first_match.offset + first_match.value.length());
  632. return matches;
  633. }
  634. Vector<Match> HexEditor::find_all_strings(size_t min_length)
  635. {
  636. if (m_document->size() == 0)
  637. return {};
  638. Vector<Match> matches;
  639. bool found_string = false;
  640. size_t offset = 0;
  641. StringBuilder builder;
  642. for (size_t i = 0; i < m_document->size(); i++) {
  643. char c = m_document->get(i).value;
  644. if (isprint(c)) {
  645. if (!found_string) {
  646. offset = i;
  647. found_string = true;
  648. }
  649. builder.append(c);
  650. } else {
  651. if (builder.length() >= min_length)
  652. matches.append({ offset, builder.to_string() });
  653. builder.clear();
  654. found_string = false;
  655. }
  656. }
  657. if (matches.is_empty())
  658. return {};
  659. auto first_match = matches.at(0);
  660. highlight(first_match.offset, first_match.offset + first_match.value.length());
  661. return matches;
  662. }
  663. void HexEditor::reset_cursor_blink_state()
  664. {
  665. m_cursor_blink_active = true;
  666. m_blink_timer->restart();
  667. }