TextEditorWidget.cpp 11 KB

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