HexEditorWidget.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 "HexEditorWidget.h"
  10. #include "FindDialog.h"
  11. #include "GoToOffsetDialog.h"
  12. #include "SearchResultsModel.h"
  13. #include "ValueInspectorModel.h"
  14. #include <AK/Optional.h>
  15. #include <AK/StringBuilder.h>
  16. #include <Applications/HexEditor/HexEditorWindowGML.h>
  17. #include <LibConfig/Client.h>
  18. #include <LibCore/File.h>
  19. #include <LibFileSystemAccessClient/Client.h>
  20. #include <LibGUI/Action.h>
  21. #include <LibGUI/BoxLayout.h>
  22. #include <LibGUI/Button.h>
  23. #include <LibGUI/FilePicker.h>
  24. #include <LibGUI/InputBox.h>
  25. #include <LibGUI/Menu.h>
  26. #include <LibGUI/Menubar.h>
  27. #include <LibGUI/MessageBox.h>
  28. #include <LibGUI/Model.h>
  29. #include <LibGUI/Statusbar.h>
  30. #include <LibGUI/TableView.h>
  31. #include <LibGUI/TextBox.h>
  32. #include <LibGUI/Toolbar.h>
  33. #include <LibGUI/ToolbarContainer.h>
  34. #include <string.h>
  35. REGISTER_WIDGET(HexEditor, HexEditor);
  36. HexEditorWidget::HexEditorWidget()
  37. {
  38. load_from_gml(hex_editor_window_gml);
  39. m_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  40. m_toolbar_container = *find_descendant_of_type_named<GUI::ToolbarContainer>("toolbar_container");
  41. m_editor = *find_descendant_of_type_named<HexEditor>("editor");
  42. m_statusbar = *find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  43. m_search_results = *find_descendant_of_type_named<GUI::TableView>("search_results");
  44. m_search_results_container = *find_descendant_of_type_named<GUI::Widget>("search_results_container");
  45. m_side_panel_container = *find_descendant_of_type_named<GUI::Widget>("side_panel_container");
  46. m_value_inspector_container = *find_descendant_of_type_named<GUI::Widget>("value_inspector_container");
  47. m_value_inspector = *find_descendant_of_type_named<GUI::TableView>("value_inspector");
  48. m_value_inspector->on_activation = [this](GUI::ModelIndex const& index) {
  49. if (!index.is_valid())
  50. return;
  51. m_selecting_from_inspector = true;
  52. m_editor->set_selection(m_editor->selection_start_offset(), index.data(GUI::ModelRole::Custom).to_integer<size_t>());
  53. m_editor->update();
  54. };
  55. m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) {
  56. m_statusbar->set_text(0, String::formatted("Offset: {:#08X}", position));
  57. m_statusbar->set_text(1, String::formatted("Edit Mode: {}", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
  58. m_statusbar->set_text(2, String::formatted("Selection Start: {}", selection_start));
  59. m_statusbar->set_text(3, String::formatted("Selection End: {}", selection_end));
  60. m_statusbar->set_text(4, String::formatted("Selected Bytes: {}", m_editor->selection_size()));
  61. bool has_selection = m_editor->has_selection();
  62. m_copy_hex_action->set_enabled(has_selection);
  63. m_copy_text_action->set_enabled(has_selection);
  64. m_copy_as_c_code_action->set_enabled(has_selection);
  65. m_fill_selection_action->set_enabled(has_selection);
  66. if (m_value_inspector_container->is_visible() && !m_selecting_from_inspector) {
  67. update_inspector_values(selection_start);
  68. }
  69. m_selecting_from_inspector = false;
  70. };
  71. m_editor->on_change = [this] {
  72. window()->set_modified(true);
  73. };
  74. m_search_results->set_activates_on_selection(true);
  75. m_search_results->on_activation = [this](const GUI::ModelIndex& index) {
  76. if (!index.is_valid())
  77. return;
  78. auto offset = index.data(GUI::ModelRole::Custom).to_i32();
  79. m_last_found_index = offset;
  80. m_editor->set_position(offset);
  81. m_editor->update();
  82. };
  83. m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new.png").release_value_but_fixme_should_propagate_errors(), [this](const GUI::Action&) {
  84. String value;
  85. if (request_close() && GUI::InputBox::show(window(), value, "Enter new file size:", "New file size") == GUI::InputBox::ExecOK && !value.is_empty()) {
  86. auto file_size = value.to_int();
  87. if (file_size.has_value() && file_size.value() > 0) {
  88. window()->set_modified(false);
  89. if (!m_editor->open_new_file(file_size.value())) {
  90. GUI::MessageBox::show(window(), "Entered file size is too large.", "Error", GUI::MessageBox::Type::Error);
  91. return;
  92. }
  93. set_path({});
  94. } else {
  95. GUI::MessageBox::show(window(), "Invalid file size entered.", "Error", GUI::MessageBox::Type::Error);
  96. }
  97. }
  98. });
  99. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  100. if (!request_close())
  101. return;
  102. auto response = FileSystemAccessClient::Client::the().try_open_file(window(), {}, Core::StandardPaths::home_directory(), Core::OpenMode::ReadWrite);
  103. if (response.is_error())
  104. return;
  105. open_file(response.value());
  106. });
  107. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  108. if (m_path.is_empty())
  109. return m_save_as_action->activate();
  110. if (!m_editor->save()) {
  111. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  112. } else {
  113. window()->set_modified(false);
  114. m_editor->update();
  115. }
  116. return;
  117. });
  118. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  119. auto response = FileSystemAccessClient::Client::the().try_save_file(window(), m_name, m_extension, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate);
  120. if (response.is_error())
  121. return;
  122. auto file = response.release_value();
  123. if (!m_editor->save_as(file)) {
  124. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  125. return;
  126. }
  127. window()->set_modified(false);
  128. set_path(file->filename());
  129. dbgln("Wrote document to {}", file->filename());
  130. });
  131. m_find_action = GUI::Action::create("&Find", { Mod_Ctrl, Key_F }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
  132. auto old_buffer = m_search_buffer;
  133. bool find_all = false;
  134. if (FindDialog::show(window(), m_search_text, m_search_buffer, find_all) == GUI::InputBox::ExecOK) {
  135. if (find_all) {
  136. auto matches = m_editor->find_all(m_search_buffer, 0);
  137. m_search_results->set_model(*new SearchResultsModel(move(matches)));
  138. m_search_results->update();
  139. if (matches.is_empty()) {
  140. GUI::MessageBox::show(window(), String::formatted("Pattern \"{}\" not found in this file", m_search_text), "Not found", GUI::MessageBox::Type::Warning);
  141. return;
  142. }
  143. GUI::MessageBox::show(window(), String::formatted("Found {} matches for \"{}\" in this file", matches.size(), m_search_text), String::formatted("{} matches", matches.size()), GUI::MessageBox::Type::Warning);
  144. set_search_results_visible(true);
  145. } else {
  146. bool same_buffers = false;
  147. if (old_buffer.size() == m_search_buffer.size()) {
  148. if (memcmp(old_buffer.data(), m_search_buffer.data(), old_buffer.size()) == 0)
  149. same_buffers = true;
  150. }
  151. auto result = m_editor->find_and_highlight(m_search_buffer, same_buffers ? last_found_index() : 0);
  152. if (!result.has_value()) {
  153. GUI::MessageBox::show(window(), String::formatted("Pattern \"{}\" not found in this file", m_search_text), "Not found", GUI::MessageBox::Type::Warning);
  154. return;
  155. }
  156. m_last_found_index = result.value();
  157. }
  158. m_editor->update();
  159. }
  160. });
  161. m_goto_offset_action = GUI::Action::create("&Go to Offset ...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-to.png").release_value_but_fixme_should_propagate_errors(), [this](const GUI::Action&) {
  162. int new_offset;
  163. auto result = GoToOffsetDialog::show(
  164. window(),
  165. m_goto_history,
  166. new_offset,
  167. m_editor->selection_start_offset(),
  168. m_editor->buffer_size());
  169. if (result == GUI::InputBox::ExecOK) {
  170. m_editor->highlight(new_offset, new_offset);
  171. m_editor->update();
  172. }
  173. });
  174. m_layout_toolbar_action = GUI::Action::create_checkable("&Toolbar", [&](auto& action) {
  175. m_toolbar_container->set_visible(action.is_checked());
  176. Config::write_bool("HexEditor", "Layout", "ShowToolbar", action.is_checked());
  177. });
  178. m_layout_search_results_action = GUI::Action::create_checkable("&Search Results", [&](auto& action) {
  179. set_search_results_visible(action.is_checked());
  180. });
  181. m_copy_hex_action = GUI::Action::create("Copy &Hex", { Mod_Ctrl, Key_C }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/hex.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
  182. m_editor->copy_selected_hex_to_clipboard();
  183. });
  184. m_copy_hex_action->set_enabled(false);
  185. m_copy_text_action = GUI::Action::create("Copy &Text", { Mod_Ctrl | Mod_Shift, Key_C }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-copy.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
  186. m_editor->copy_selected_text_to_clipboard();
  187. });
  188. m_copy_text_action->set_enabled(false);
  189. m_copy_as_c_code_action = GUI::Action::create("Copy as &C Code", { Mod_Alt | Mod_Shift, Key_C }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/c.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
  190. m_editor->copy_selected_hex_to_clipboard_as_c_code();
  191. });
  192. m_copy_as_c_code_action->set_enabled(false);
  193. m_fill_selection_action = GUI::Action::create("Fill &Selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
  194. String value;
  195. if (GUI::InputBox::show(window(), value, "Fill byte (hex):", "Fill Selection") == GUI::InputBox::ExecOK && !value.is_empty()) {
  196. auto fill_byte = strtol(value.characters(), nullptr, 16);
  197. m_editor->fill_selection(fill_byte);
  198. }
  199. });
  200. m_fill_selection_action->set_enabled(false);
  201. m_layout_value_inspector_action = GUI::Action::create_checkable("&Value Inspector", [&](auto& action) {
  202. set_value_inspector_visible(action.is_checked());
  203. });
  204. m_toolbar->add_action(*m_new_action);
  205. m_toolbar->add_action(*m_open_action);
  206. m_toolbar->add_action(*m_save_action);
  207. m_toolbar->add_separator();
  208. m_toolbar->add_action(*m_find_action);
  209. m_toolbar->add_action(*m_goto_offset_action);
  210. m_statusbar->segment(0).set_clickable(true);
  211. m_statusbar->segment(0).set_action(*m_goto_offset_action);
  212. m_editor->set_focus(true);
  213. }
  214. void HexEditorWidget::update_inspector_values(size_t position)
  215. {
  216. // build out primitive types like u8, i8, u16, etc
  217. size_t byte_read_count = 0;
  218. u64 unsigned_64_bit_int = 0;
  219. for (int i = 0; i < 8; ++i) {
  220. Optional<u8> read_result = m_editor->get_byte(position + i);
  221. u8 current_byte = 0;
  222. if (!read_result.has_value())
  223. break;
  224. current_byte = read_result.release_value();
  225. if (m_value_inspector_little_endian)
  226. unsigned_64_bit_int = ((u64)current_byte << (8 * byte_read_count)) + unsigned_64_bit_int;
  227. else
  228. unsigned_64_bit_int = (unsigned_64_bit_int << 8) + current_byte;
  229. ++byte_read_count;
  230. }
  231. if (!m_value_inspector_little_endian) {
  232. // if we didn't read far enough, lets finish shifting the bytes so the code below works
  233. size_t bytes_left_to_read = 8 - byte_read_count;
  234. unsigned_64_bit_int = (unsigned_64_bit_int << (8 * bytes_left_to_read));
  235. }
  236. // Populate the model
  237. NonnullRefPtr<ValueInspectorModel> value_inspector_model = make_ref_counted<ValueInspectorModel>(m_value_inspector_little_endian);
  238. if (byte_read_count >= 1) {
  239. u8 unsigned_byte_value = 0;
  240. if (m_value_inspector_little_endian)
  241. unsigned_byte_value = (unsigned_64_bit_int & 0xFF);
  242. else
  243. unsigned_byte_value = (unsigned_64_bit_int >> (64 - 8)) & 0xFF;
  244. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedByte, String::number(static_cast<i8>(unsigned_byte_value)));
  245. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedByte, String::number(unsigned_byte_value));
  246. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::ASCII, String::formatted("{:c}", static_cast<char>(unsigned_byte_value)));
  247. } else {
  248. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedByte, "");
  249. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedByte, "");
  250. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::ASCII, "");
  251. }
  252. if (byte_read_count >= 2) {
  253. u16 unsigned_short_value = 0;
  254. if (m_value_inspector_little_endian)
  255. unsigned_short_value = (unsigned_64_bit_int & 0xFFFF);
  256. else
  257. unsigned_short_value = (unsigned_64_bit_int >> (64 - 16)) & 0xFFFF;
  258. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedShort, String::number(static_cast<i16>(unsigned_short_value)));
  259. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedShort, String::number(unsigned_short_value));
  260. } else {
  261. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedShort, "");
  262. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedShort, "");
  263. }
  264. if (byte_read_count >= 4) {
  265. u32 unsigned_int_value = 0;
  266. if (m_value_inspector_little_endian)
  267. unsigned_int_value = (unsigned_64_bit_int & 0xFFFFFFFF);
  268. else
  269. unsigned_int_value = (unsigned_64_bit_int >> 32) & 0xFFFFFFFF;
  270. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedInt, String::number(static_cast<i32>(unsigned_int_value)));
  271. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedInt, String::number(unsigned_int_value));
  272. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::Float, String::number(bit_cast<float>(unsigned_int_value)));
  273. } else {
  274. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedInt, "");
  275. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedInt, "");
  276. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::Float, "");
  277. }
  278. if (byte_read_count >= 8) {
  279. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedLong, String::number(static_cast<i64>(unsigned_64_bit_int)));
  280. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedLong, String::number(unsigned_64_bit_int));
  281. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::Double, String::number(bit_cast<double>(unsigned_64_bit_int)));
  282. } else {
  283. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::SignedLong, "");
  284. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::UnsignedLong, "");
  285. value_inspector_model->set_parsed_value(ValueInspectorModel::ValueType::Double, "");
  286. }
  287. // FIXME: Parse as other values like ASCII, UTF8, UTF16, Timestamp etc
  288. m_value_inspector->set_model(value_inspector_model);
  289. m_value_inspector->update();
  290. }
  291. void HexEditorWidget::initialize_menubar(GUI::Window& window)
  292. {
  293. auto& file_menu = window.add_menu("&File");
  294. file_menu.add_action(*m_new_action);
  295. file_menu.add_action(*m_open_action);
  296. file_menu.add_action(*m_save_action);
  297. file_menu.add_action(*m_save_as_action);
  298. file_menu.add_separator();
  299. file_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  300. if (!request_close())
  301. return;
  302. GUI::Application::the()->quit();
  303. }));
  304. auto& edit_menu = window.add_menu("&Edit");
  305. edit_menu.add_action(GUI::CommonActions::make_select_all_action([this](auto&) {
  306. m_editor->select_all();
  307. m_editor->update();
  308. }));
  309. edit_menu.add_action(*m_fill_selection_action);
  310. edit_menu.add_separator();
  311. edit_menu.add_action(*m_copy_hex_action);
  312. edit_menu.add_action(*m_copy_text_action);
  313. edit_menu.add_action(*m_copy_as_c_code_action);
  314. edit_menu.add_separator();
  315. edit_menu.add_action(*m_find_action);
  316. edit_menu.add_action(GUI::Action::create("Find &Next", { Mod_None, Key_F3 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find-next.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
  317. if (m_search_text.is_empty() || m_search_buffer.is_empty()) {
  318. GUI::MessageBox::show(&window, "Nothing to search for", "Not found", GUI::MessageBox::Type::Warning);
  319. return;
  320. }
  321. auto result = m_editor->find_and_highlight(m_search_buffer, last_found_index());
  322. if (!result.has_value()) {
  323. GUI::MessageBox::show(&window, String::formatted("No more matches for \"{}\" found in this file", m_search_text), "Not found", GUI::MessageBox::Type::Warning);
  324. return;
  325. }
  326. m_editor->update();
  327. m_last_found_index = result.value();
  328. }));
  329. edit_menu.add_action(GUI::Action::create("Find All &Strings", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors(), [&](const GUI::Action&) {
  330. int min_length = 4;
  331. auto matches = m_editor->find_all_strings(min_length);
  332. m_search_results->set_model(*new SearchResultsModel(move(matches)));
  333. m_search_results->update();
  334. if (matches.is_empty()) {
  335. GUI::MessageBox::show(&window, "No strings found in this file", "Not found", GUI::MessageBox::Type::Warning);
  336. return;
  337. }
  338. set_search_results_visible(true);
  339. m_editor->update();
  340. }));
  341. edit_menu.add_separator();
  342. edit_menu.add_action(*m_goto_offset_action);
  343. auto& view_menu = window.add_menu("&View");
  344. auto show_toolbar = Config::read_bool("HexEditor", "Layout", "ShowToolbar", true);
  345. m_layout_toolbar_action->set_checked(show_toolbar);
  346. m_toolbar_container->set_visible(show_toolbar);
  347. view_menu.add_action(*m_layout_toolbar_action);
  348. view_menu.add_action(*m_layout_search_results_action);
  349. view_menu.add_action(*m_layout_value_inspector_action);
  350. view_menu.add_separator();
  351. auto bytes_per_row = Config::read_i32("HexEditor", "Layout", "BytesPerRow", 16);
  352. m_editor->set_bytes_per_row(bytes_per_row);
  353. m_editor->update();
  354. m_bytes_per_row_actions.set_exclusive(true);
  355. auto& bytes_per_row_menu = view_menu.add_submenu("Bytes per &Row");
  356. for (int i = 8; i <= 32; i += 8) {
  357. auto action = GUI::Action::create_checkable(String::number(i), [this, i](auto&) {
  358. m_editor->set_bytes_per_row(i);
  359. m_editor->update();
  360. Config::write_i32("HexEditor", "Layout", "BytesPerRow", i);
  361. });
  362. m_bytes_per_row_actions.add_action(action);
  363. bytes_per_row_menu.add_action(action);
  364. if (i == bytes_per_row)
  365. action->set_checked(true);
  366. }
  367. m_value_inspector_mode_actions.set_exclusive(true);
  368. auto& inspector_mode_menu = view_menu.add_submenu("Value Inspector &Mode");
  369. auto little_endian_mode = GUI::Action::create_checkable("&Little Endian", [&](auto& action) {
  370. m_value_inspector_little_endian = action.is_checked();
  371. update_inspector_values(m_editor->selection_start_offset());
  372. });
  373. m_value_inspector_mode_actions.add_action(little_endian_mode);
  374. inspector_mode_menu.add_action(little_endian_mode);
  375. auto big_endian_mode = GUI::Action::create_checkable("&Big Endian", [this](auto& action) {
  376. m_value_inspector_little_endian = !action.is_checked();
  377. update_inspector_values(m_editor->selection_start_offset());
  378. });
  379. m_value_inspector_mode_actions.add_action(big_endian_mode);
  380. inspector_mode_menu.add_action(big_endian_mode);
  381. // Default to little endian mode
  382. little_endian_mode->set_checked(true);
  383. auto& help_menu = window.add_menu("&Help");
  384. help_menu.add_action(GUI::CommonActions::make_about_action("Hex Editor", GUI::Icon::default_icon("app-hex-editor"), &window));
  385. }
  386. void HexEditorWidget::set_path(StringView path)
  387. {
  388. if (path.is_empty()) {
  389. m_path = {};
  390. m_name = {};
  391. m_extension = {};
  392. } else {
  393. auto lexical_path = LexicalPath(path);
  394. m_path = lexical_path.string();
  395. m_name = lexical_path.title();
  396. m_extension = lexical_path.extension();
  397. }
  398. update_title();
  399. }
  400. void HexEditorWidget::update_title()
  401. {
  402. StringBuilder builder;
  403. if (m_path.is_empty())
  404. builder.append("Untitled");
  405. else
  406. builder.append(m_path);
  407. builder.append("[*] - Hex Editor");
  408. window()->set_title(builder.to_string());
  409. }
  410. void HexEditorWidget::open_file(NonnullRefPtr<Core::File> file)
  411. {
  412. window()->set_modified(false);
  413. m_editor->open_file(file);
  414. set_path(file->filename());
  415. }
  416. bool HexEditorWidget::request_close()
  417. {
  418. if (!window()->is_modified())
  419. return true;
  420. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path);
  421. if (result == GUI::MessageBox::ExecYes) {
  422. m_save_action->activate();
  423. return !window()->is_modified();
  424. }
  425. return result == GUI::MessageBox::ExecNo;
  426. }
  427. void HexEditorWidget::set_search_results_visible(bool visible)
  428. {
  429. m_layout_search_results_action->set_checked(visible);
  430. m_search_results_container->set_visible(visible);
  431. // Ensure side panel container is visible if either search result or value inspector are turned on
  432. m_side_panel_container->set_visible(visible || m_value_inspector_container->is_visible());
  433. }
  434. void HexEditorWidget::set_value_inspector_visible(bool visible)
  435. {
  436. if (visible)
  437. update_inspector_values(m_editor->selection_start_offset());
  438. m_layout_value_inspector_action->set_checked(visible);
  439. m_value_inspector_container->set_visible(visible);
  440. // Ensure side panel container is visible if either search result or value inspector are turned on
  441. m_side_panel_container->set_visible(visible || m_search_results_container->is_visible());
  442. }
  443. void HexEditorWidget::drop_event(GUI::DropEvent& event)
  444. {
  445. event.accept();
  446. if (event.mime_data().has_urls()) {
  447. auto urls = event.mime_data().urls();
  448. if (urls.is_empty())
  449. return;
  450. window()->move_to_front();
  451. // TODO: A drop event should be considered user consent for opening a file
  452. auto response = FileSystemAccessClient::Client::the().try_request_file(window(), urls.first().path(), Core::OpenMode::ReadOnly);
  453. if (response.is_error())
  454. return;
  455. open_file(response.value());
  456. }
  457. }