TextEditorWidget.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "TextEditorWidget.h"
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <AK/Optional.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/URL.h>
  32. #include <Applications/TextEditor/TextEditorWindowGML.h>
  33. #include <LibCore/ConfigFile.h>
  34. #include <LibCore/File.h>
  35. #include <LibCore/MimeData.h>
  36. #include <LibCpp/SyntaxHighlighter.h>
  37. #include <LibDesktop/Launcher.h>
  38. #include <LibGUI/Action.h>
  39. #include <LibGUI/ActionGroup.h>
  40. #include <LibGUI/BoxLayout.h>
  41. #include <LibGUI/Button.h>
  42. #include <LibGUI/FilePicker.h>
  43. #include <LibGUI/FontPicker.h>
  44. #include <LibGUI/GMLSyntaxHighlighter.h>
  45. #include <LibGUI/INISyntaxHighlighter.h>
  46. #include <LibGUI/Menu.h>
  47. #include <LibGUI/MenuBar.h>
  48. #include <LibGUI/MessageBox.h>
  49. #include <LibGUI/RegularEditingEngine.h>
  50. #include <LibGUI/Splitter.h>
  51. #include <LibGUI/StatusBar.h>
  52. #include <LibGUI/TextBox.h>
  53. #include <LibGUI/TextEditor.h>
  54. #include <LibGUI/ToolBar.h>
  55. #include <LibGUI/ToolBarContainer.h>
  56. #include <LibGUI/VimEditingEngine.h>
  57. #include <LibGfx/Font.h>
  58. #include <LibJS/SyntaxHighlighter.h>
  59. #include <LibMarkdown/Document.h>
  60. #include <LibWeb/OutOfProcessWebView.h>
  61. #include <Shell/SyntaxHighlighter.h>
  62. #include <string.h>
  63. TextEditorWidget::TextEditorWidget()
  64. {
  65. load_from_gml(text_editor_window_gml);
  66. m_config = Core::ConfigFile::get_for_app("TextEditor");
  67. auto& toolbar = *find_descendant_of_type_named<GUI::ToolBar>("toolbar");
  68. auto& toolbar_container = *find_descendant_of_type_named<GUI::ToolBarContainer>("toolbar_container");
  69. m_editor = *find_descendant_of_type_named<GUI::TextEditor>("editor");
  70. m_editor->set_ruler_visible(true);
  71. m_editor->set_automatic_indentation_enabled(true);
  72. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  73. m_editor->on_change = [this] {
  74. update_preview();
  75. // Do not mark as dirty on the first change (When document is first opened.)
  76. if (m_document_opening) {
  77. m_document_opening = false;
  78. return;
  79. }
  80. bool was_dirty = m_document_dirty;
  81. m_document_dirty = true;
  82. if (!was_dirty)
  83. update_title();
  84. };
  85. m_page_view = *find_descendant_of_type_named<Web::OutOfProcessWebView>("webview");
  86. m_page_view->on_link_hover = [this](auto& url) {
  87. if (url.is_valid())
  88. m_statusbar->set_text(url.to_string());
  89. else
  90. update_statusbar_cursor_position();
  91. };
  92. m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  93. if (!Desktop::Launcher::open(url)) {
  94. GUI::MessageBox::show(
  95. window(),
  96. String::formatted("The link to '{}' could not be opened.", url),
  97. "Failed to open link",
  98. GUI::MessageBox::Type::Error);
  99. }
  100. };
  101. m_find_replace_widget = *find_descendant_of_type_named<GUI::Widget>("find_replace_widget");
  102. m_find_widget = *find_descendant_of_type_named<GUI::Widget>("find_widget");
  103. m_replace_widget = *find_descendant_of_type_named<GUI::Widget>("replace_widget");
  104. m_find_textbox = m_find_widget->add<GUI::TextBox>();
  105. m_replace_textbox = m_replace_widget->add<GUI::TextBox>();
  106. m_find_next_action = GUI::Action::create("Find next", { Mod_Ctrl, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find-next.png"), [&](auto&) {
  107. auto needle = m_find_textbox->text();
  108. if (needle.is_empty()) {
  109. dbgln("find_next(\"\")");
  110. return;
  111. }
  112. if (m_find_use_regex)
  113. m_editor->document().update_regex_matches(needle);
  114. auto found_range = m_editor->document().find_next(needle, m_editor->normalized_selection().end(), GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  115. dbgln("find_next('{}') returned {}", needle, found_range);
  116. if (found_range.is_valid()) {
  117. m_editor->set_selection(found_range);
  118. } else {
  119. GUI::MessageBox::show(window(),
  120. String::formatted("Not found: \"{}\"", needle),
  121. "Not found",
  122. GUI::MessageBox::Type::Information);
  123. }
  124. });
  125. m_find_regex_action = GUI::Action::create("Find regex", { Mod_Ctrl | Mod_Shift, Key_R }, [&](auto&) {
  126. m_find_regex_button->set_checked(!m_find_regex_button->is_checked());
  127. m_find_use_regex = m_find_regex_button->is_checked();
  128. });
  129. m_find_previous_action = GUI::Action::create("Find previous", { Mod_Ctrl | Mod_Shift, Key_G }, [&](auto&) {
  130. auto needle = m_find_textbox->text();
  131. if (needle.is_empty()) {
  132. dbgln("find_prev(\"\")");
  133. return;
  134. }
  135. auto selection_start = m_editor->normalized_selection().start();
  136. if (!selection_start.is_valid())
  137. selection_start = m_editor->normalized_selection().end();
  138. if (m_find_use_regex)
  139. m_editor->document().update_regex_matches(needle);
  140. auto found_range = m_editor->document().find_previous(needle, selection_start, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  141. dbgln("find_prev(\"{}\") returned {}", needle, found_range);
  142. if (found_range.is_valid()) {
  143. m_editor->set_selection(found_range);
  144. } else {
  145. GUI::MessageBox::show(window(),
  146. String::formatted("Not found: \"{}\"", needle),
  147. "Not found",
  148. GUI::MessageBox::Type::Information);
  149. }
  150. });
  151. m_replace_next_action = GUI::Action::create("Replace next", { Mod_Ctrl, Key_F1 }, [&](auto&) {
  152. auto needle = m_find_textbox->text();
  153. auto substitute = m_replace_textbox->text();
  154. if (needle.is_empty())
  155. return;
  156. auto selection_start = m_editor->normalized_selection().start();
  157. if (!selection_start.is_valid())
  158. selection_start = m_editor->normalized_selection().start();
  159. if (m_find_use_regex)
  160. m_editor->document().update_regex_matches(needle);
  161. auto found_range = m_editor->document().find_next(needle, selection_start, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  162. if (found_range.is_valid()) {
  163. m_editor->set_selection(found_range);
  164. m_editor->insert_at_cursor_or_replace_selection(substitute);
  165. } else {
  166. GUI::MessageBox::show(window(),
  167. String::formatted("Not found: \"{}\"", needle),
  168. "Not found",
  169. GUI::MessageBox::Type::Information);
  170. }
  171. });
  172. m_replace_previous_action = GUI::Action::create("Replace previous", { Mod_Ctrl | Mod_Shift, Key_F1 }, [&](auto&) {
  173. auto needle = m_find_textbox->text();
  174. auto substitute = m_replace_textbox->text();
  175. if (needle.is_empty())
  176. return;
  177. auto selection_start = m_editor->normalized_selection().start();
  178. if (!selection_start.is_valid())
  179. selection_start = m_editor->normalized_selection().start();
  180. if (m_find_use_regex)
  181. m_editor->document().update_regex_matches(needle);
  182. auto found_range = m_editor->document().find_previous(needle, selection_start);
  183. if (found_range.is_valid()) {
  184. m_editor->set_selection(found_range);
  185. m_editor->insert_at_cursor_or_replace_selection(substitute);
  186. } else {
  187. GUI::MessageBox::show(window(),
  188. String::formatted("Not found: \"{}\"", needle),
  189. "Not found",
  190. GUI::MessageBox::Type::Information);
  191. }
  192. });
  193. m_replace_all_action = GUI::Action::create("Replace all", { Mod_Ctrl, Key_F2 }, [&](auto&) {
  194. auto needle = m_find_textbox->text();
  195. auto substitute = m_replace_textbox->text();
  196. if (needle.is_empty())
  197. return;
  198. if (m_find_use_regex)
  199. m_editor->document().update_regex_matches(needle);
  200. auto found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  201. while (found_range.is_valid()) {
  202. m_editor->set_selection(found_range);
  203. m_editor->insert_at_cursor_or_replace_selection(substitute);
  204. found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  205. }
  206. });
  207. m_find_previous_button = *find_descendant_of_type_named<GUI::Button>("find_previous_button");
  208. m_find_previous_button->set_action(*m_find_previous_action);
  209. m_find_next_button = *find_descendant_of_type_named<GUI::Button>("find_next_button");
  210. m_find_next_button->set_action(*m_find_next_action);
  211. m_find_textbox->on_return_pressed = [this] {
  212. m_find_next_button->click();
  213. };
  214. m_find_regex_button = m_find_widget->add<GUI::Button>(".*");
  215. m_find_regex_button->set_fixed_width(20);
  216. m_find_regex_button->set_action(*m_find_regex_action);
  217. m_find_textbox->on_escape_pressed = [this] {
  218. m_find_replace_widget->set_visible(false);
  219. m_editor->set_focus(true);
  220. };
  221. m_replace_previous_button = *find_descendant_of_type_named<GUI::Button>("replace_previous_button");
  222. m_replace_previous_button->set_action(*m_replace_previous_action);
  223. m_replace_next_button = *find_descendant_of_type_named<GUI::Button>("replace_next_button");
  224. m_replace_next_button->set_action(*m_replace_next_action);
  225. m_replace_all_button = *find_descendant_of_type_named<GUI::Button>("replace_all_button");
  226. m_replace_all_button->set_action(*m_replace_all_action);
  227. m_replace_textbox->on_return_pressed = [this] {
  228. m_replace_next_button->click();
  229. };
  230. m_replace_textbox->on_escape_pressed = [this] {
  231. m_find_replace_widget->set_visible(false);
  232. m_editor->set_focus(true);
  233. };
  234. m_vim_emulation_setting_action = GUI::Action::create_checkable("Vim emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
  235. if (action.is_checked())
  236. m_editor->set_editing_engine(make<GUI::VimEditingEngine>());
  237. else
  238. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  239. });
  240. m_vim_emulation_setting_action->set_checked(false);
  241. m_find_replace_action = GUI::Action::create("Find/Replace...", { Mod_Ctrl, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"), [this](auto&) {
  242. m_find_replace_widget->set_visible(true);
  243. m_find_widget->set_visible(true);
  244. m_replace_widget->set_visible(true);
  245. m_find_textbox->set_focus(true);
  246. if (m_editor->has_selection()) {
  247. auto selected_text = m_editor->document().text_in_range(m_editor->normalized_selection());
  248. m_find_textbox->set_text(selected_text);
  249. }
  250. m_find_textbox->select_all();
  251. });
  252. m_editor->add_custom_context_menu_action(*m_find_replace_action);
  253. m_editor->add_custom_context_menu_action(*m_find_next_action);
  254. m_editor->add_custom_context_menu_action(*m_find_previous_action);
  255. m_statusbar = *find_descendant_of_type_named<GUI::StatusBar>("statusbar");
  256. m_editor->on_cursor_change = [this] { update_statusbar_cursor_position(); };
  257. m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GUI::Action&) {
  258. if (m_document_dirty) {
  259. auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  260. if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
  261. m_save_action->activate();
  262. if (save_document_first_result == GUI::Dialog::ExecResult::ExecCancel)
  263. return;
  264. }
  265. m_document_dirty = false;
  266. m_editor->set_text(StringView());
  267. set_path(LexicalPath());
  268. update_title();
  269. });
  270. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  271. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
  272. if (!open_path.has_value())
  273. return;
  274. if (m_document_dirty) {
  275. auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  276. if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
  277. m_save_action->activate();
  278. if (save_document_first_result == GUI::Dialog::ExecResult::ExecCancel)
  279. return;
  280. }
  281. open_sesame(open_path.value());
  282. });
  283. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  284. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
  285. if (!save_path.has_value())
  286. return;
  287. if (!m_editor->write_to_file(save_path.value())) {
  288. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  289. return;
  290. }
  291. m_document_dirty = false;
  292. set_path(LexicalPath(save_path.value()));
  293. dbgln("Wrote document to {}", save_path.value());
  294. });
  295. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  296. if (!m_path.is_empty()) {
  297. if (!m_editor->write_to_file(m_path)) {
  298. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  299. } else {
  300. m_document_dirty = false;
  301. update_title();
  302. }
  303. return;
  304. }
  305. m_save_as_action->activate();
  306. });
  307. auto menubar = GUI::MenuBar::construct();
  308. auto& app_menu = menubar->add_menu("Text Editor");
  309. app_menu.add_action(*m_new_action);
  310. app_menu.add_action(*m_open_action);
  311. app_menu.add_action(*m_save_action);
  312. app_menu.add_action(*m_save_as_action);
  313. app_menu.add_separator();
  314. app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  315. if (!request_close())
  316. return;
  317. GUI::Application::the()->quit();
  318. }));
  319. auto& edit_menu = menubar->add_menu("Edit");
  320. edit_menu.add_action(m_editor->undo_action());
  321. edit_menu.add_action(m_editor->redo_action());
  322. edit_menu.add_separator();
  323. edit_menu.add_action(m_editor->cut_action());
  324. edit_menu.add_action(m_editor->copy_action());
  325. edit_menu.add_action(m_editor->paste_action());
  326. edit_menu.add_action(m_editor->delete_action());
  327. edit_menu.add_separator();
  328. edit_menu.add_action(*m_vim_emulation_setting_action);
  329. edit_menu.add_separator();
  330. edit_menu.add_action(*m_find_replace_action);
  331. edit_menu.add_action(*m_find_next_action);
  332. edit_menu.add_action(*m_find_regex_action);
  333. edit_menu.add_action(*m_find_previous_action);
  334. edit_menu.add_action(*m_replace_next_action);
  335. edit_menu.add_action(*m_replace_previous_action);
  336. edit_menu.add_action(*m_replace_all_action);
  337. m_no_preview_action = GUI::Action::create_checkable(
  338. "No preview", [this](auto&) {
  339. set_preview_mode(PreviewMode::None);
  340. });
  341. m_markdown_preview_action = GUI::Action::create_checkable(
  342. "Markdown preview", [this](auto&) {
  343. set_preview_mode(PreviewMode::Markdown);
  344. },
  345. this);
  346. m_html_preview_action = GUI::Action::create_checkable(
  347. "HTML preview", [this](auto&) {
  348. set_preview_mode(PreviewMode::HTML);
  349. },
  350. this);
  351. m_preview_actions.add_action(*m_no_preview_action);
  352. m_preview_actions.add_action(*m_markdown_preview_action);
  353. m_preview_actions.add_action(*m_html_preview_action);
  354. m_preview_actions.set_exclusive(true);
  355. m_layout_toolbar_action = GUI::Action::create_checkable("Toolbar", [&](auto& action) {
  356. action.is_checked() ? toolbar_container.set_visible(true) : toolbar_container.set_visible(false);
  357. m_config->write_bool_entry("Layout", "ShowToolbar", action.is_checked());
  358. m_config->sync();
  359. });
  360. auto show_toolbar = m_config->read_bool_entry("Layout", "ShowToolbar", true);
  361. m_layout_toolbar_action->set_checked(show_toolbar);
  362. toolbar_container.set_visible(show_toolbar);
  363. m_layout_statusbar_action = GUI::Action::create_checkable("Status bar", [&](auto& action) {
  364. action.is_checked() ? m_statusbar->set_visible(true) : m_statusbar->set_visible(false);
  365. m_config->write_bool_entry("Layout", "ShowStatusBar", action.is_checked());
  366. m_config->sync();
  367. });
  368. auto show_statusbar = m_config->read_bool_entry("Layout", "ShowStatusBar", true);
  369. m_layout_statusbar_action->set_checked(show_statusbar);
  370. m_statusbar->set_visible(show_statusbar);
  371. m_layout_ruler_action = GUI::Action::create_checkable("Ruler", [&](auto& action) {
  372. action.is_checked() ? m_editor->set_ruler_visible(true) : m_editor->set_ruler_visible(false);
  373. m_config->write_bool_entry("Layout", "ShowRuler", action.is_checked());
  374. m_config->sync();
  375. });
  376. auto show_ruler = m_config->read_bool_entry("Layout", "ShowRuler", true);
  377. m_layout_ruler_action->set_checked(show_ruler);
  378. m_editor->set_ruler_visible(show_ruler);
  379. auto& view_menu = menubar->add_menu("View");
  380. auto& layout_menu = view_menu.add_submenu("Layout");
  381. layout_menu.add_action(*m_layout_toolbar_action);
  382. layout_menu.add_action(*m_layout_statusbar_action);
  383. layout_menu.add_action(*m_layout_ruler_action);
  384. view_menu.add_separator();
  385. view_menu.add_action(GUI::Action::create("Editor font...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"),
  386. [&](auto&) {
  387. auto picker = GUI::FontPicker::construct(window(), &m_editor->font(), false);
  388. if (picker->exec() == GUI::Dialog::ExecOK) {
  389. dbgln("setting font {}", picker->font()->qualified_name());
  390. m_editor->set_font(picker->font());
  391. }
  392. }));
  393. view_menu.add_separator();
  394. m_wrapping_mode_actions.set_exclusive(true);
  395. auto& wrapping_mode_menu = view_menu.add_submenu("Wrapping mode");
  396. m_no_wrapping_action = GUI::Action::create_checkable("No wrapping", [&](auto&) {
  397. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  398. });
  399. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap anywhere", [&](auto&) {
  400. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  401. });
  402. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at words", [&](auto&) {
  403. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  404. });
  405. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  406. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  407. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  408. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  409. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  410. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  411. m_no_wrapping_action->set_checked(true);
  412. view_menu.add_separator();
  413. view_menu.add_action(*m_no_preview_action);
  414. view_menu.add_action(*m_markdown_preview_action);
  415. view_menu.add_action(*m_html_preview_action);
  416. m_no_preview_action->set_checked(true);
  417. view_menu.add_separator();
  418. syntax_actions.set_exclusive(true);
  419. auto& syntax_menu = view_menu.add_submenu("Syntax");
  420. m_plain_text_highlight = GUI::Action::create_checkable("Plain text", [&](auto&) {
  421. m_editor->set_syntax_highlighter({});
  422. m_editor->update();
  423. });
  424. m_plain_text_highlight->set_checked(true);
  425. syntax_actions.add_action(*m_plain_text_highlight);
  426. syntax_menu.add_action(*m_plain_text_highlight);
  427. m_cpp_highlight = GUI::Action::create_checkable("C++", [&](auto&) {
  428. m_editor->set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
  429. m_editor->update();
  430. });
  431. syntax_actions.add_action(*m_cpp_highlight);
  432. syntax_menu.add_action(*m_cpp_highlight);
  433. m_js_highlight = GUI::Action::create_checkable("JavaScript", [&](auto&) {
  434. m_editor->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  435. m_editor->update();
  436. });
  437. syntax_actions.add_action(*m_js_highlight);
  438. syntax_menu.add_action(*m_js_highlight);
  439. m_gml_highlight = GUI::Action::create_checkable("GML", [&](auto&) {
  440. m_editor->set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
  441. m_editor->update();
  442. });
  443. syntax_actions.add_action(*m_gml_highlight);
  444. syntax_menu.add_action(*m_gml_highlight);
  445. m_ini_highlight = GUI::Action::create_checkable("INI File", [&](auto&) {
  446. m_editor->set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  447. m_editor->update();
  448. });
  449. syntax_actions.add_action(*m_ini_highlight);
  450. syntax_menu.add_action(*m_ini_highlight);
  451. m_shell_highlight = GUI::Action::create_checkable("Shell File", [&](auto&) {
  452. m_editor->set_syntax_highlighter(make<Shell::SyntaxHighlighter>());
  453. m_editor->update();
  454. });
  455. syntax_actions.add_action(*m_shell_highlight);
  456. syntax_menu.add_action(*m_shell_highlight);
  457. auto& help_menu = menubar->add_menu("Help");
  458. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  459. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/TextEditor.md"), "/bin/Help");
  460. }));
  461. help_menu.add_action(GUI::CommonActions::make_about_action("Text Editor", GUI::Icon::default_icon("app-text-editor"), window()));
  462. GUI::Application::the()->set_menubar(move(menubar));
  463. toolbar.add_action(*m_new_action);
  464. toolbar.add_action(*m_open_action);
  465. toolbar.add_action(*m_save_action);
  466. toolbar.add_separator();
  467. toolbar.add_action(m_editor->cut_action());
  468. toolbar.add_action(m_editor->copy_action());
  469. toolbar.add_action(m_editor->paste_action());
  470. toolbar.add_action(m_editor->delete_action());
  471. toolbar.add_separator();
  472. toolbar.add_action(m_editor->undo_action());
  473. toolbar.add_action(m_editor->redo_action());
  474. }
  475. TextEditorWidget::~TextEditorWidget()
  476. {
  477. }
  478. void TextEditorWidget::set_path(const LexicalPath& lexical_path)
  479. {
  480. m_path = lexical_path.string();
  481. m_name = lexical_path.title();
  482. m_extension = lexical_path.extension();
  483. if (m_extension == "c" || m_extension == "cc" || m_extension == "cxx" || m_extension == "cpp" || m_extension == "h") {
  484. m_cpp_highlight->activate();
  485. } else if (m_extension == "js" || m_extension == "json") {
  486. m_js_highlight->activate();
  487. } else if (m_extension == "gml") {
  488. m_gml_highlight->activate();
  489. } else if (m_extension == "ini") {
  490. m_ini_highlight->activate();
  491. } else {
  492. m_plain_text_highlight->activate();
  493. }
  494. if (m_auto_detect_preview_mode) {
  495. if (m_extension == "md")
  496. set_preview_mode(PreviewMode::Markdown);
  497. else if (m_extension == "html")
  498. set_preview_mode(PreviewMode::HTML);
  499. else
  500. set_preview_mode(PreviewMode::None);
  501. }
  502. update_title();
  503. }
  504. void TextEditorWidget::update_title()
  505. {
  506. StringBuilder builder;
  507. if (m_path.is_empty())
  508. builder.append("Untitled");
  509. else
  510. builder.append(m_path);
  511. if (m_document_dirty)
  512. builder.append(" (*)");
  513. builder.append(" - Text Editor");
  514. window()->set_title(builder.to_string());
  515. }
  516. void TextEditorWidget::open_sesame(const String& path)
  517. {
  518. auto file = Core::File::construct(path);
  519. if (!file->open(Core::IODevice::ReadOnly) && file->error() != ENOENT) {
  520. GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  521. return;
  522. }
  523. m_editor->set_text(file->read_all());
  524. m_document_dirty = false;
  525. m_document_opening = true;
  526. set_path(LexicalPath(path));
  527. m_editor->set_focus(true);
  528. }
  529. bool TextEditorWidget::request_close()
  530. {
  531. if (!m_document_dirty)
  532. return true;
  533. auto result = GUI::MessageBox::show(window(), "The document has been modified. Would you like to save?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  534. if (result == GUI::MessageBox::ExecYes) {
  535. m_save_action->activate();
  536. return true;
  537. }
  538. if (result == GUI::MessageBox::ExecNo)
  539. return true;
  540. return false;
  541. }
  542. void TextEditorWidget::drop_event(GUI::DropEvent& event)
  543. {
  544. event.accept();
  545. window()->move_to_front();
  546. if (event.mime_data().has_urls()) {
  547. auto urls = event.mime_data().urls();
  548. if (urls.is_empty())
  549. return;
  550. if (urls.size() > 1) {
  551. GUI::MessageBox::show(window(), "TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error);
  552. return;
  553. }
  554. open_sesame(urls.first().path());
  555. }
  556. }
  557. void TextEditorWidget::set_preview_mode(PreviewMode mode)
  558. {
  559. if (m_preview_mode == mode)
  560. return;
  561. m_preview_mode = mode;
  562. if (m_preview_mode == PreviewMode::HTML) {
  563. m_html_preview_action->set_checked(true);
  564. m_page_view->set_visible(true);
  565. update_html_preview();
  566. } else if (m_preview_mode == PreviewMode::Markdown) {
  567. m_markdown_preview_action->set_checked(true);
  568. m_page_view->set_visible(true);
  569. update_markdown_preview();
  570. } else {
  571. m_no_preview_action->set_checked(true);
  572. m_page_view->set_visible(false);
  573. }
  574. }
  575. void TextEditorWidget::update_preview()
  576. {
  577. switch (m_preview_mode) {
  578. case PreviewMode::Markdown:
  579. update_markdown_preview();
  580. break;
  581. case PreviewMode::HTML:
  582. update_html_preview();
  583. break;
  584. default:
  585. break;
  586. }
  587. }
  588. void TextEditorWidget::update_markdown_preview()
  589. {
  590. auto document = Markdown::Document::parse(m_editor->text());
  591. if (document) {
  592. auto html = document->render_to_html();
  593. auto current_scroll_pos = m_page_view->visible_content_rect();
  594. m_page_view->load_html(html, URL::create_with_file_protocol(m_path));
  595. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  596. }
  597. }
  598. void TextEditorWidget::update_html_preview()
  599. {
  600. auto current_scroll_pos = m_page_view->visible_content_rect();
  601. m_page_view->load_html(m_editor->text(), URL::create_with_file_protocol(m_path));
  602. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  603. }
  604. void TextEditorWidget::update_statusbar_cursor_position()
  605. {
  606. StringBuilder builder;
  607. builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
  608. m_statusbar->set_text(builder.to_string());
  609. }