TextEditorWidget.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "TextEditorWidget.h"
  2. #include <AK/Optional.h>
  3. #include <AK/StringBuilder.h>
  4. #include <LibCore/CFile.h>
  5. #include <LibGUI/GAction.h>
  6. #include <LibGUI/GBoxLayout.h>
  7. #include <LibGUI/GButton.h>
  8. #include <LibGUI/GFilePicker.h>
  9. #include <LibGUI/GFontDatabase.h>
  10. #include <LibGUI/GMenuBar.h>
  11. #include <LibGUI/GMessageBox.h>
  12. #include <LibGUI/GStatusBar.h>
  13. #include <LibGUI/GTextBox.h>
  14. #include <LibGUI/GTextEditor.h>
  15. #include <LibGUI/GToolBar.h>
  16. TextEditorWidget::TextEditorWidget()
  17. {
  18. set_layout(make<GBoxLayout>(Orientation::Vertical));
  19. layout()->set_spacing(0);
  20. auto* toolbar = new GToolBar(this);
  21. m_editor = new GTextEditor(GTextEditor::MultiLine, this);
  22. m_editor->set_ruler_visible(true);
  23. m_editor->set_automatic_indentation_enabled(true);
  24. m_find_widget = new GWidget(this);
  25. m_find_widget->set_fill_with_background_color(true);
  26. m_find_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  27. m_find_widget->set_preferred_size(0, 22);
  28. m_find_widget->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  29. m_find_widget->layout()->set_margins({ 2, 2, 2, 2 });
  30. m_find_widget->set_visible(false);
  31. m_find_textbox = new GTextBox(m_find_widget);
  32. m_find_next_action = GAction::create("Find next", { Mod_Ctrl, Key_G }, [&](auto&) {
  33. auto needle = m_find_textbox->text();
  34. auto found_range = m_editor->find_next(needle, m_editor->normalized_selection().end());
  35. dbg() << "find_next(\"" << needle << "\") returned " << found_range;
  36. if (found_range.is_valid()) {
  37. m_editor->set_selection(found_range);
  38. } else {
  39. GMessageBox::show(
  40. String::format("Not found: \"%s\"", needle.characters()),
  41. "Not found",
  42. GMessageBox::Type::Information,
  43. GMessageBox::InputType::OK, window());
  44. }
  45. });
  46. m_find_previous_action = GAction::create("Find previous", { Mod_Ctrl | Mod_Shift, Key_G }, [&](auto&) {
  47. auto needle = m_find_textbox->text();
  48. auto selection_start = m_editor->normalized_selection().start();
  49. if (!selection_start.is_valid())
  50. selection_start = m_editor->normalized_selection().end();
  51. auto found_range = m_editor->find_prev(needle, selection_start);
  52. dbg() << "find_prev(\"" << needle << "\") returned " << found_range;
  53. if (found_range.is_valid()) {
  54. m_editor->set_selection(found_range);
  55. } else {
  56. GMessageBox::show(
  57. String::format("Not found: \"%s\"", needle.characters()),
  58. "Not found",
  59. GMessageBox::Type::Information,
  60. GMessageBox::InputType::OK, window());
  61. }
  62. });
  63. m_find_previous_button = new GButton("Previous", m_find_widget);
  64. m_find_previous_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  65. m_find_previous_button->set_preferred_size(64, 0);
  66. m_find_previous_button->set_action(*m_find_previous_action);
  67. m_find_next_button = new GButton("Next", m_find_widget);
  68. m_find_next_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  69. m_find_next_button->set_preferred_size(64, 0);
  70. m_find_next_button->set_action(*m_find_next_action);
  71. m_find_textbox->on_return_pressed = [this] {
  72. m_find_next_button->click();
  73. };
  74. m_find_textbox->on_escape_pressed = [this] {
  75. m_find_widget->set_visible(false);
  76. m_editor->set_focus(true);
  77. };
  78. m_find_action = GAction::create("Find...", { Mod_Ctrl, Key_F }, [this](auto&) {
  79. m_find_widget->set_visible(true);
  80. m_find_textbox->set_focus(true);
  81. m_find_textbox->select_all();
  82. });
  83. m_editor->add_custom_context_menu_action(*m_find_action);
  84. m_editor->add_custom_context_menu_action(*m_find_next_action);
  85. m_editor->add_custom_context_menu_action(*m_find_previous_action);
  86. auto* statusbar = new GStatusBar(this);
  87. m_editor->on_cursor_change = [statusbar, this] {
  88. StringBuilder builder;
  89. builder.appendf("Line: %d, Column: %d", m_editor->cursor().line() + 1, m_editor->cursor().column());
  90. statusbar->set_text(builder.to_string());
  91. };
  92. m_new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [](const GAction&) {
  93. dbgprintf("FIXME: Implement File/New\n");
  94. });
  95. m_open_action = GAction::create("Open...", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) {
  96. Optional<String> open_path = GFilePicker::get_open_filepath();
  97. if (!open_path.has_value())
  98. return;
  99. open_sesame(open_path.value());
  100. });
  101. m_save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
  102. Optional<String> save_path = GFilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
  103. if (!save_path.has_value())
  104. return;
  105. if (!m_editor->write_to_file(save_path.value())) {
  106. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  107. return;
  108. }
  109. set_path(FileSystemPath(save_path.value()));
  110. dbg() << "Wrote document to " << save_path.value();
  111. });
  112. m_save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) {
  113. if (!m_path.is_empty()) {
  114. if (!m_editor->write_to_file(m_path))
  115. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  116. return;
  117. }
  118. m_save_as_action->activate();
  119. });
  120. m_line_wrapping_setting_action = GAction::create("Line wrapping", [&](GAction& action) {
  121. action.set_checked(!action.is_checked());
  122. m_editor->set_line_wrapping_enabled(action.is_checked());
  123. });
  124. m_line_wrapping_setting_action->set_checkable(true);
  125. m_line_wrapping_setting_action->set_checked(m_editor->is_line_wrapping_enabled());
  126. auto menubar = make<GMenuBar>();
  127. auto app_menu = make<GMenu>("Text Editor");
  128. app_menu->add_action(*m_new_action);
  129. app_menu->add_action(*m_open_action);
  130. app_menu->add_action(*m_save_action);
  131. app_menu->add_action(*m_save_as_action);
  132. app_menu->add_separator();
  133. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
  134. GApplication::the().quit(0);
  135. return;
  136. }));
  137. menubar->add_menu(move(app_menu));
  138. auto edit_menu = make<GMenu>("Edit");
  139. edit_menu->add_action(m_editor->undo_action());
  140. edit_menu->add_action(m_editor->redo_action());
  141. edit_menu->add_separator();
  142. edit_menu->add_action(m_editor->cut_action());
  143. edit_menu->add_action(m_editor->copy_action());
  144. edit_menu->add_action(m_editor->paste_action());
  145. edit_menu->add_action(m_editor->delete_action());
  146. edit_menu->add_separator();
  147. edit_menu->add_action(*m_find_action);
  148. edit_menu->add_action(*m_find_next_action);
  149. edit_menu->add_action(*m_find_previous_action);
  150. menubar->add_menu(move(edit_menu));
  151. auto font_menu = make<GMenu>("Font");
  152. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  153. font_menu->add_action(GAction::create(font_name, [this](const GAction& action) {
  154. m_editor->set_font(GFontDatabase::the().get_by_name(action.text()));
  155. m_editor->update();
  156. }));
  157. });
  158. menubar->add_menu(move(font_menu));
  159. auto view_menu = make<GMenu>("View");
  160. view_menu->add_action(*m_line_wrapping_setting_action);
  161. menubar->add_menu(move(view_menu));
  162. auto help_menu = make<GMenu>("Help");
  163. help_menu->add_action(GAction::create("About", [](const GAction&) {
  164. dbgprintf("FIXME: Implement Help/About\n");
  165. }));
  166. menubar->add_menu(move(help_menu));
  167. GApplication::the().set_menubar(move(menubar));
  168. toolbar->add_action(*m_new_action);
  169. toolbar->add_action(*m_open_action);
  170. toolbar->add_action(*m_save_action);
  171. toolbar->add_separator();
  172. toolbar->add_action(m_editor->cut_action());
  173. toolbar->add_action(m_editor->copy_action());
  174. toolbar->add_action(m_editor->paste_action());
  175. toolbar->add_action(m_editor->delete_action());
  176. toolbar->add_separator();
  177. toolbar->add_action(m_editor->undo_action());
  178. toolbar->add_action(m_editor->redo_action());
  179. m_editor->set_focus(true);
  180. }
  181. TextEditorWidget::~TextEditorWidget()
  182. {
  183. }
  184. void TextEditorWidget::set_path(const FileSystemPath& file)
  185. {
  186. m_path = file.string();
  187. m_name = file.title();
  188. m_extension = file.extension();
  189. StringBuilder builder;
  190. builder.append("Text Editor: ");
  191. builder.append(file.string());
  192. window()->set_title(builder.to_string());
  193. }
  194. void TextEditorWidget::open_sesame(const String& path)
  195. {
  196. CFile file(path);
  197. if (!file.open(CIODevice::ReadOnly)) {
  198. GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  199. return;
  200. }
  201. m_editor->set_text(file.read_all());
  202. set_path(FileSystemPath(path));
  203. }