HexEditorWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "HexEditorWidget.h"
  27. #include <AK/Optional.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/File.h>
  30. #include <LibGUI/GAboutDialog.h>
  31. #include <LibGUI/GAction.h>
  32. #include <LibGUI/GBoxLayout.h>
  33. #include <LibGUI/GButton.h>
  34. #include <LibGUI/GFilePicker.h>
  35. #include <LibGUI/GFontDatabase.h>
  36. #include <LibGUI/GInputBox.h>
  37. #include <LibGUI/GMenuBar.h>
  38. #include <LibGUI/GMessageBox.h>
  39. #include <LibGUI/GStatusBar.h>
  40. #include <LibGUI/GTextBox.h>
  41. #include <LibGUI/GTextEditor.h>
  42. #include <LibGUI/GToolBar.h>
  43. #include <stdio.h>
  44. HexEditorWidget::HexEditorWidget()
  45. {
  46. set_layout(make<GUI::VerticalBoxLayout>());
  47. layout()->set_spacing(0);
  48. m_editor = HexEditor::construct(this);
  49. m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) {
  50. m_statusbar->set_text(0, String::format("Offset: %8X", position));
  51. m_statusbar->set_text(1, String::format("Edit Mode: %s", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
  52. m_statusbar->set_text(2, String::format("Selection Start: %d", selection_start));
  53. m_statusbar->set_text(3, String::format("Selection End: %d", selection_end));
  54. m_statusbar->set_text(4, String::format("Selected Bytes: %d", (selection_end - selection_start) + 1));
  55. };
  56. m_editor->on_change = [this] {
  57. bool was_dirty = m_document_dirty;
  58. m_document_dirty = true;
  59. if (!was_dirty)
  60. update_title();
  61. };
  62. m_statusbar = GUI::StatusBar::construct(5, this);
  63. m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
  64. if (m_document_dirty) {
  65. auto save_document_first_box = GUI::MessageBox::construct("Save Document First?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel, window());
  66. auto save_document_first_result = save_document_first_box->exec();
  67. if (save_document_first_result != GUI::Dialog::ExecResult::ExecOK)
  68. return;
  69. m_save_action->activate();
  70. }
  71. auto input_box = GUI::InputBox::construct("Enter new file size:", "New file size", this);
  72. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
  73. auto valid = false;
  74. auto file_size = input_box->text_value().to_int(valid);
  75. if (valid && file_size > 0) {
  76. m_document_dirty = false;
  77. m_editor->set_buffer(ByteBuffer::create_zeroed(file_size));
  78. set_path(FileSystemPath());
  79. update_title();
  80. } else {
  81. GUI::MessageBox::show("Invalid file size entered.", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  82. }
  83. }
  84. });
  85. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  86. Optional<String> open_path = GUI::FilePicker::get_open_filepath();
  87. if (!open_path.has_value())
  88. return;
  89. open_file(open_path.value());
  90. });
  91. m_save_action = GUI::Action::create("Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GUI::Action&) {
  92. if (!m_path.is_empty()) {
  93. if (!m_editor->write_to_file(m_path)) {
  94. GUI::MessageBox::show("Unable to save file.\n", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  95. } else {
  96. m_document_dirty = false;
  97. update_title();
  98. }
  99. return;
  100. }
  101. m_save_as_action->activate();
  102. });
  103. m_save_as_action = GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GUI::Action&) {
  104. Optional<String> save_path = GUI::FilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "bin" : m_extension);
  105. if (!save_path.has_value())
  106. return;
  107. if (!m_editor->write_to_file(save_path.value())) {
  108. GUI::MessageBox::show("Unable to save file.\n", "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  109. return;
  110. }
  111. m_document_dirty = false;
  112. set_path(FileSystemPath(save_path.value()));
  113. dbg() << "Wrote document to " << save_path.value();
  114. });
  115. auto menubar = make<GUI::MenuBar>();
  116. auto app_menu = GUI::Menu::construct("Hex Editor");
  117. app_menu->add_action(*m_new_action);
  118. app_menu->add_action(*m_open_action);
  119. app_menu->add_action(*m_save_action);
  120. app_menu->add_action(*m_save_as_action);
  121. app_menu->add_separator();
  122. app_menu->add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  123. if (!request_close())
  124. return;
  125. GUI::Application::the().quit(0);
  126. }));
  127. menubar->add_menu(move(app_menu));
  128. auto bytes_per_row_menu = GUI::Menu::construct("Bytes Per Row");
  129. for (int i = 8; i <= 32; i += 8) {
  130. bytes_per_row_menu->add_action(GUI::Action::create(String::number(i), [this, i](auto&) {
  131. m_editor->set_bytes_per_row(i);
  132. m_editor->update();
  133. }));
  134. }
  135. m_goto_decimal_offset_action = GUI::Action::create("Go To Offset (Decimal)...", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
  136. auto input_box = GUI::InputBox::construct("Enter Decimal offset:", "Go To", this);
  137. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
  138. auto valid = false;
  139. auto new_offset = input_box->text_value().to_int(valid);
  140. if (valid) {
  141. m_editor->set_position(new_offset);
  142. }
  143. }
  144. });
  145. m_goto_hex_offset_action = GUI::Action::create("Go To Offset (Hex)...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
  146. auto input_box = GUI::InputBox::construct("Enter Hex offset:", "Go To", this);
  147. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
  148. auto new_offset = strtol(input_box->text_value().characters(), nullptr, 16);
  149. m_editor->set_position(new_offset);
  150. }
  151. });
  152. auto edit_menu = GUI::Menu::construct("Edit");
  153. edit_menu->add_action(GUI::Action::create("Fill selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
  154. auto input_box = GUI::InputBox::construct("Fill byte (hex):", "Fill Selection", this);
  155. if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
  156. auto fill_byte = strtol(input_box->text_value().characters(), nullptr, 16);
  157. m_editor->fill_selection(fill_byte);
  158. }
  159. }));
  160. edit_menu->add_separator();
  161. edit_menu->add_action(*m_goto_decimal_offset_action);
  162. edit_menu->add_action(*m_goto_hex_offset_action);
  163. edit_menu->add_separator();
  164. edit_menu->add_action(GUI::Action::create("Copy Hex", { Mod_Ctrl, Key_C }, [&](const GUI::Action&) {
  165. m_editor->copy_selected_hex_to_clipboard();
  166. }));
  167. edit_menu->add_action(GUI::Action::create("Copy Text", { Mod_Ctrl | Mod_Shift, Key_C }, [&](const GUI::Action&) {
  168. m_editor->copy_selected_text_to_clipboard();
  169. }));
  170. edit_menu->add_separator();
  171. edit_menu->add_action(GUI::Action::create("Copy As C Code", { Mod_Alt | Mod_Shift, Key_C }, [&](const GUI::Action&) {
  172. m_editor->copy_selected_hex_to_clipboard_as_c_code();
  173. }));
  174. menubar->add_menu(move(edit_menu));
  175. auto view_menu = GUI::Menu::construct("View");
  176. view_menu->add_submenu(move(bytes_per_row_menu));
  177. menubar->add_menu(move(view_menu));
  178. auto help_menu = GUI::Menu::construct("Help");
  179. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  180. GUI::AboutDialog::show("Hex Editor", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hexeditor.png"), window());
  181. }));
  182. menubar->add_menu(move(help_menu));
  183. GUI::Application::the().set_menubar(move(menubar));
  184. m_editor->set_focus(true);
  185. }
  186. HexEditorWidget::~HexEditorWidget()
  187. {
  188. }
  189. void HexEditorWidget::set_path(const FileSystemPath& file)
  190. {
  191. m_path = file.string();
  192. m_name = file.title();
  193. m_extension = file.extension();
  194. update_title();
  195. }
  196. void HexEditorWidget::update_title()
  197. {
  198. StringBuilder builder;
  199. builder.append("Hex Editor: ");
  200. builder.append(m_path);
  201. if (m_document_dirty)
  202. builder.append(" (*)");
  203. window()->set_title(builder.to_string());
  204. }
  205. void HexEditorWidget::open_file(const String& path)
  206. {
  207. auto file = Core::File::construct(path);
  208. if (!file->open(Core::IODevice::ReadOnly)) {
  209. GUI::MessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  210. return;
  211. }
  212. m_document_dirty = false;
  213. m_editor->set_buffer(file->read_all()); // FIXME: On really huge files, this is never going to work. Should really create a framework to fetch data from the file on-demand.
  214. set_path(FileSystemPath(path));
  215. }
  216. bool HexEditorWidget::request_close()
  217. {
  218. if (!m_document_dirty)
  219. return true;
  220. auto result = GUI::MessageBox::show("The file has been modified. Quit without saving?", "Quit without saving?", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel, window());
  221. return result == GUI::MessageBox::ExecOK;
  222. }