TextEditorWidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = new GToolBar(this);
  23. m_editor = new GTextEditor(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 = new GWidget(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 = new GTextBox(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 }, [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. auto* statusbar = new GStatusBar(this);
  96. m_editor->on_cursor_change = [statusbar, this] {
  97. StringBuilder builder;
  98. builder.appendf("Line: %d, Column: %d", m_editor->cursor().line() + 1, m_editor->cursor().column());
  99. 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"), [](const GAction&) {
  102. dbgprintf("FIXME: Implement File/New\n");
  103. });
  104. m_open_action = GAction::create("Open...", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) {
  105. Optional<String> open_path = GFilePicker::get_open_filepath();
  106. if (!open_path.has_value())
  107. return;
  108. open_sesame(open_path.value());
  109. });
  110. m_save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
  111. Optional<String> save_path = GFilePicker::get_save_filepath(m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
  112. if (!save_path.has_value())
  113. return;
  114. if (!m_editor->write_to_file(save_path.value())) {
  115. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  116. return;
  117. }
  118. m_document_dirty = false;
  119. set_path(FileSystemPath(save_path.value()));
  120. dbg() << "Wrote document to " << save_path.value();
  121. });
  122. m_save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) {
  123. if (!m_path.is_empty()) {
  124. if (!m_editor->write_to_file(m_path)) {
  125. GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  126. } else {
  127. m_document_dirty = false;
  128. update_title();
  129. }
  130. return;
  131. }
  132. m_save_as_action->activate();
  133. });
  134. m_line_wrapping_setting_action = GAction::create("Line wrapping", [&](GAction& action) {
  135. action.set_checked(!action.is_checked());
  136. m_editor->set_line_wrapping_enabled(action.is_checked());
  137. });
  138. m_line_wrapping_setting_action->set_checkable(true);
  139. m_line_wrapping_setting_action->set_checked(m_editor->is_line_wrapping_enabled());
  140. auto menubar = make<GMenuBar>();
  141. auto app_menu = make<GMenu>("Text Editor");
  142. app_menu->add_action(*m_new_action);
  143. app_menu->add_action(*m_open_action);
  144. app_menu->add_action(*m_save_action);
  145. app_menu->add_action(*m_save_as_action);
  146. app_menu->add_separator();
  147. app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [this](const GAction&) {
  148. if (!request_close())
  149. return;
  150. GApplication::the().quit(0);
  151. }));
  152. menubar->add_menu(move(app_menu));
  153. auto edit_menu = make<GMenu>("Edit");
  154. edit_menu->add_action(m_editor->undo_action());
  155. edit_menu->add_action(m_editor->redo_action());
  156. edit_menu->add_separator();
  157. edit_menu->add_action(m_editor->cut_action());
  158. edit_menu->add_action(m_editor->copy_action());
  159. edit_menu->add_action(m_editor->paste_action());
  160. edit_menu->add_action(m_editor->delete_action());
  161. edit_menu->add_separator();
  162. edit_menu->add_action(*m_find_action);
  163. edit_menu->add_action(*m_find_next_action);
  164. edit_menu->add_action(*m_find_previous_action);
  165. menubar->add_menu(move(edit_menu));
  166. auto font_menu = make<GMenu>("Font");
  167. GFontDatabase::the().for_each_fixed_width_font([&](const StringView& font_name) {
  168. font_menu->add_action(GAction::create(font_name, [this](const GAction& action) {
  169. m_editor->set_font(GFontDatabase::the().get_by_name(action.text()));
  170. m_editor->update();
  171. }));
  172. });
  173. auto view_menu = make<GMenu>("View");
  174. view_menu->add_action(*m_line_wrapping_setting_action);
  175. view_menu->add_separator();
  176. view_menu->add_submenu(move(font_menu));
  177. menubar->add_menu(move(view_menu));
  178. auto help_menu = make<GMenu>("Help");
  179. help_menu->add_action(GAction::create("About", [&](const GAction&) {
  180. GAboutDialog::show("TextEditor", load_png("/res/icons/32x32/app-texteditor.png"), window());
  181. }));
  182. menubar->add_menu(move(help_menu));
  183. GApplication::the().set_menubar(move(menubar));
  184. toolbar->add_action(*m_new_action);
  185. toolbar->add_action(*m_open_action);
  186. toolbar->add_action(*m_save_action);
  187. toolbar->add_separator();
  188. toolbar->add_action(m_editor->cut_action());
  189. toolbar->add_action(m_editor->copy_action());
  190. toolbar->add_action(m_editor->paste_action());
  191. toolbar->add_action(m_editor->delete_action());
  192. toolbar->add_separator();
  193. toolbar->add_action(m_editor->undo_action());
  194. toolbar->add_action(m_editor->redo_action());
  195. m_editor->set_focus(true);
  196. }
  197. TextEditorWidget::~TextEditorWidget()
  198. {
  199. }
  200. void TextEditorWidget::set_path(const FileSystemPath& file)
  201. {
  202. m_path = file.string();
  203. m_name = file.title();
  204. m_extension = file.extension();
  205. update_title();
  206. }
  207. void TextEditorWidget::update_title()
  208. {
  209. StringBuilder builder;
  210. builder.append("Text Editor: ");
  211. builder.append(m_path);
  212. if (m_document_dirty)
  213. builder.append(" (*)");
  214. window()->set_title(builder.to_string());
  215. }
  216. void TextEditorWidget::open_sesame(const String& path)
  217. {
  218. CFile file(path);
  219. if (!file.open(CIODevice::ReadOnly)) {
  220. GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
  221. return;
  222. }
  223. m_document_dirty = false;
  224. m_editor->set_text(file.read_all());
  225. set_path(FileSystemPath(path));
  226. }
  227. bool TextEditorWidget::request_close()
  228. {
  229. if (!m_document_dirty)
  230. return true;
  231. GMessageBox box("The document has been modified. Quit without saving?", "Quit without saving?", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel, window());
  232. auto result = box.exec();
  233. return result == GMessageBox::ExecOK;
  234. }