TextEditorWidget.cpp 9.7 KB

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