HexEditorWidget.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "HexEditorWidget.h"
  2. #include <AK/Optional.h>
  3. #include <AK/StringBuilder.h>
  4. #include <LibCore/CFile.h>
  5. #include <LibDraw/PNGLoader.h>
  6. #include <LibGUI/GAboutDialog.h>
  7. #include <LibGUI/GAction.h>
  8. #include <LibGUI/GBoxLayout.h>
  9. #include <LibGUI/GButton.h>
  10. #include <LibGUI/GFilePicker.h>
  11. #include <LibGUI/GFontDatabase.h>
  12. #include <LibGUI/GInputBox.h>
  13. #include <LibGUI/GMenuBar.h>
  14. #include <LibGUI/GMessageBox.h>
  15. #include <LibGUI/GStatusBar.h>
  16. #include <LibGUI/GTextBox.h>
  17. #include <LibGUI/GTextEditor.h>
  18. #include <LibGUI/GToolBar.h>
  19. #include <stdio.h>
  20. HexEditorWidget::HexEditorWidget()
  21. {
  22. set_layout(make<GBoxLayout>(Orientation::Vertical));
  23. layout()->set_spacing(0);
  24. m_editor = HexEditor::construct(this);
  25. m_editor->on_status_change = [this](int position, HexEditor::EditMode edit_mode, int selection_start, int selection_end) {
  26. m_statusbar->set_text(0, String::format("Offset: %8X", position));
  27. m_statusbar->set_text(1, String::format("Edit Mode: %s", edit_mode == HexEditor::EditMode::Hex ? "Hex" : "Text"));
  28. m_statusbar->set_text(2, String::format("Selection Start: %d", selection_start));
  29. m_statusbar->set_text(3, String::format("Selection End: %d", selection_end));
  30. m_statusbar->set_text(4, String::format("Selected Bytes: %d", (selection_end - selection_start) + 1));
  31. };
  32. m_editor->on_change = [this] {
  33. bool was_dirty = m_document_dirty;
  34. m_document_dirty = true;
  35. if (!was_dirty)
  36. update_title();
  37. };
  38. m_statusbar = GStatusBar::construct(5, this);
  39. m_new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GAction&) {
  40. if (m_document_dirty) {
  41. auto save_document_first_box = GMessageBox::construct("Save Document First?", "Warning", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel, window());
  42. auto save_document_first_result = save_document_first_box->exec();
  43. if (save_document_first_result != GDialog::ExecResult::ExecOK)
  44. return;
  45. m_save_action->activate();
  46. }
  47. auto input_box = GInputBox::construct("Enter new file size:", "New file size", this);
  48. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  49. auto valid = false;
  50. auto file_size = input_box->text_value().to_int(valid);
  51. if (valid && file_size > 0) {
  52. m_document_dirty = false;
  53. m_editor->set_buffer(ByteBuffer::create_zeroed(file_size));
  54. set_path(FileSystemPath());
  55. update_title();
  56. } else {
  57. GMessageBox::show("Invalid file size entered.", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  58. }
  59. }
  60. });
  61. m_open_action = GCommonActions::make_open_action([this](auto&) {
  62. Optional<String> open_path = GFilePicker::get_open_filepath();
  63. if (!open_path.has_value())
  64. return;
  65. open_file(open_path.value());
  66. });
  67. m_save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) {
  68. if (!m_path.is_empty()) {
  69. if (!m_editor->write_to_file(m_path)) {
  70. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  71. } else {
  72. m_document_dirty = false;
  73. update_title();
  74. }
  75. return;
  76. }
  77. m_save_as_action->activate();
  78. });
  79. m_save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
  80. Optional<String> save_path = GFilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "bin" : m_extension);
  81. if (!save_path.has_value())
  82. return;
  83. if (!m_editor->write_to_file(save_path.value())) {
  84. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  85. return;
  86. }
  87. m_document_dirty = false;
  88. set_path(FileSystemPath(save_path.value()));
  89. dbg() << "Wrote document to " << save_path.value();
  90. });
  91. auto menubar = make<GMenuBar>();
  92. auto app_menu = GMenu::construct("Hex Editor");
  93. app_menu->add_action(*m_new_action);
  94. app_menu->add_action(*m_open_action);
  95. app_menu->add_action(*m_save_action);
  96. app_menu->add_action(*m_save_as_action);
  97. app_menu->add_separator();
  98. app_menu->add_action(GCommonActions::make_quit_action([this](auto&) {
  99. if (!request_close())
  100. return;
  101. GApplication::the().quit(0);
  102. }));
  103. menubar->add_menu(move(app_menu));
  104. auto bytes_per_row_menu = GMenu::construct("Bytes Per Row");
  105. for (int i = 8; i <= 32; i += 8) {
  106. bytes_per_row_menu->add_action(GAction::create(String::number(i), [this, i](auto&) {
  107. m_editor->set_bytes_per_row(i);
  108. m_editor->update();
  109. }));
  110. }
  111. m_goto_decimal_offset_action = GAction::create("Go To Offset (Decimal)...", GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GAction&) {
  112. auto input_box = GInputBox::construct("Enter offset:", "Go To", this);
  113. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  114. auto valid = false;
  115. auto new_offset = input_box->text_value().to_int(valid);
  116. if (valid) {
  117. m_editor->set_position(new_offset);
  118. }
  119. }
  120. });
  121. m_goto_hex_offset_action = GAction::create("Go To Offset (Hex)...", GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GAction&) {
  122. auto input_box = GInputBox::construct("Enter offset:", "Go To", this);
  123. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  124. auto new_offset = strtol(input_box->text_value().characters(), nullptr, 16);
  125. m_editor->set_position(new_offset);
  126. }
  127. });
  128. auto edit_menu = GMenu::construct("Edit");
  129. edit_menu->add_action(GAction::create("Fill selection...", [&](const GAction&) {
  130. auto input_box = GInputBox::construct("Fill byte (hex):", "Fill Selection", this);
  131. if (input_box->exec() == GInputBox::ExecOK && !input_box->text_value().is_empty()) {
  132. auto fill_byte = strtol(input_box->text_value().characters(), nullptr, 16);
  133. m_editor->fill_selection(fill_byte);
  134. }
  135. }));
  136. edit_menu->add_separator();
  137. edit_menu->add_action(*m_goto_decimal_offset_action);
  138. edit_menu->add_action(*m_goto_hex_offset_action);
  139. edit_menu->add_separator();
  140. edit_menu->add_action(GAction::create("Copy Hex", [&](const GAction&) {
  141. m_editor->copy_selected_hex_to_clipboard();
  142. }));
  143. edit_menu->add_action(GAction::create("Copy Text", [&](const GAction&) {
  144. m_editor->copy_selected_text_to_clipboard();
  145. }));
  146. edit_menu->add_separator();
  147. edit_menu->add_action(GAction::create("Copy As C Code", [&](const GAction&) {
  148. m_editor->copy_selected_hex_to_clipboard_as_c_code();
  149. }));
  150. menubar->add_menu(move(edit_menu));
  151. auto view_menu = GMenu::construct("View");
  152. view_menu->add_submenu(move(bytes_per_row_menu));
  153. menubar->add_menu(move(view_menu));
  154. auto help_menu = GMenu::construct("Help");
  155. help_menu->add_action(GAction::create("About", [&](const GAction&) {
  156. GAboutDialog::show("Hex Editor", load_png("/res/icons/32x32/app-hexeditor.png"), window());
  157. }));
  158. menubar->add_menu(move(help_menu));
  159. GApplication::the().set_menubar(move(menubar));
  160. m_editor->set_focus(true);
  161. }
  162. HexEditorWidget::~HexEditorWidget()
  163. {
  164. }
  165. void HexEditorWidget::set_path(const FileSystemPath& file)
  166. {
  167. m_path = file.string();
  168. m_name = file.title();
  169. m_extension = file.extension();
  170. update_title();
  171. }
  172. void HexEditorWidget::update_title()
  173. {
  174. StringBuilder builder;
  175. builder.append("Hex Editor: ");
  176. builder.append(m_path);
  177. if (m_document_dirty)
  178. builder.append(" (*)");
  179. window()->set_title(builder.to_string());
  180. }
  181. void HexEditorWidget::open_file(const String& path)
  182. {
  183. auto file = CFile::construct(path);
  184. if (!file->open(CIODevice::ReadOnly)) {
  185. GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  186. return;
  187. }
  188. m_document_dirty = false;
  189. 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.
  190. set_path(FileSystemPath(path));
  191. }
  192. bool HexEditorWidget::request_close()
  193. {
  194. if (!m_document_dirty)
  195. return true;
  196. auto result = GMessageBox::show("The file has been modified. Quit without saving?", "Quit without saving?", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel, window());
  197. return result == GMessageBox::ExecOK;
  198. }