TextEditorWidget.cpp 11 KB

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