TextEditorWidget.cpp 12 KB

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