TextEditorWidget.cpp 11 KB

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