TextEditorWidget.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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/File.h>
  34. #include <LibCore/MimeData.h>
  35. #include <LibCpp/SyntaxHighlighter.h>
  36. #include <LibDesktop/Launcher.h>
  37. #include <LibGUI/Action.h>
  38. #include <LibGUI/ActionGroup.h>
  39. #include <LibGUI/BoxLayout.h>
  40. #include <LibGUI/Button.h>
  41. #include <LibGUI/FilePicker.h>
  42. #include <LibGUI/FontPicker.h>
  43. #include <LibGUI/GMLSyntaxHighlighter.h>
  44. #include <LibGUI/INISyntaxHighlighter.h>
  45. #include <LibGUI/Menu.h>
  46. #include <LibGUI/MenuBar.h>
  47. #include <LibGUI/MessageBox.h>
  48. #include <LibGUI/RegularEditingEngine.h>
  49. #include <LibGUI/ShellSyntaxHighlighter.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 <string.h>
  62. TextEditorWidget::TextEditorWidget()
  63. {
  64. load_from_gml(text_editor_window_gml);
  65. auto& toolbar = *find_descendant_of_type_named<GUI::ToolBar>("toolbar");
  66. m_editor = *find_descendant_of_type_named<GUI::TextEditor>("editor");
  67. m_editor->set_ruler_visible(true);
  68. m_editor->set_automatic_indentation_enabled(true);
  69. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  70. m_editor->on_change = [this] {
  71. update_preview();
  72. // Do not mark as dirty on the first change (When document is first opened.)
  73. if (m_document_opening) {
  74. m_document_opening = false;
  75. return;
  76. }
  77. bool was_dirty = m_document_dirty;
  78. m_document_dirty = true;
  79. if (!was_dirty)
  80. update_title();
  81. };
  82. m_page_view = *find_descendant_of_type_named<Web::OutOfProcessWebView>("webview");
  83. m_page_view->on_link_hover = [this](auto& url) {
  84. if (url.is_valid())
  85. m_statusbar->set_text(url.to_string());
  86. else
  87. update_statusbar_cursor_position();
  88. };
  89. m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  90. if (!Desktop::Launcher::open(url)) {
  91. GUI::MessageBox::show(
  92. window(),
  93. String::formatted("The link to '{}' could not be opened.", url),
  94. "Failed to open link",
  95. GUI::MessageBox::Type::Error);
  96. }
  97. };
  98. m_find_replace_widget = *find_descendant_of_type_named<GUI::Widget>("find_replace_widget");
  99. m_find_widget = *find_descendant_of_type_named<GUI::Widget>("find_widget");
  100. m_replace_widget = *find_descendant_of_type_named<GUI::Widget>("replace_widget");
  101. m_find_textbox = m_find_widget->add<GUI::TextBox>();
  102. m_replace_textbox = m_replace_widget->add<GUI::TextBox>();
  103. 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&) {
  104. auto needle = m_find_textbox->text();
  105. if (needle.is_empty()) {
  106. dbgln("find_next(\"\")");
  107. return;
  108. }
  109. if (m_find_use_regex)
  110. m_editor->document().update_regex_matches(needle);
  111. auto found_range = m_editor->document().find_next(needle, m_editor->normalized_selection().end(), GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  112. dbgln("find_next('{}') returned {}", needle, found_range);
  113. if (found_range.is_valid()) {
  114. m_editor->set_selection(found_range);
  115. } else {
  116. GUI::MessageBox::show(window(),
  117. String::formatted("Not found: \"{}\"", needle),
  118. "Not found",
  119. GUI::MessageBox::Type::Information);
  120. }
  121. });
  122. m_find_regex_action = GUI::Action::create("Find regex", { Mod_Ctrl | Mod_Shift, Key_R }, [&](auto&) {
  123. m_find_regex_button->set_checked(!m_find_regex_button->is_checked());
  124. m_find_use_regex = m_find_regex_button->is_checked();
  125. });
  126. m_find_previous_action = GUI::Action::create("Find previous", { Mod_Ctrl | Mod_Shift, Key_G }, [&](auto&) {
  127. auto needle = m_find_textbox->text();
  128. if (needle.is_empty()) {
  129. dbgln("find_prev(\"\")");
  130. return;
  131. }
  132. auto selection_start = m_editor->normalized_selection().start();
  133. if (!selection_start.is_valid())
  134. selection_start = m_editor->normalized_selection().end();
  135. if (m_find_use_regex)
  136. m_editor->document().update_regex_matches(needle);
  137. auto found_range = m_editor->document().find_previous(needle, selection_start, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  138. dbgln("find_prev(\"{}\") returned {}", needle, found_range);
  139. if (found_range.is_valid()) {
  140. m_editor->set_selection(found_range);
  141. } else {
  142. GUI::MessageBox::show(window(),
  143. String::formatted("Not found: \"{}\"", needle),
  144. "Not found",
  145. GUI::MessageBox::Type::Information);
  146. }
  147. });
  148. m_replace_next_action = GUI::Action::create("Replace next", { Mod_Ctrl, Key_F1 }, [&](auto&) {
  149. auto needle = m_find_textbox->text();
  150. auto substitute = m_replace_textbox->text();
  151. if (needle.is_empty())
  152. return;
  153. auto selection_start = m_editor->normalized_selection().start();
  154. if (!selection_start.is_valid())
  155. selection_start = m_editor->normalized_selection().start();
  156. if (m_find_use_regex)
  157. m_editor->document().update_regex_matches(needle);
  158. auto found_range = m_editor->document().find_next(needle, selection_start, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  159. if (found_range.is_valid()) {
  160. m_editor->set_selection(found_range);
  161. m_editor->insert_at_cursor_or_replace_selection(substitute);
  162. } else {
  163. GUI::MessageBox::show(window(),
  164. String::formatted("Not found: \"{}\"", needle),
  165. "Not found",
  166. GUI::MessageBox::Type::Information);
  167. }
  168. });
  169. m_replace_previous_action = GUI::Action::create("Replace previous", { Mod_Ctrl | Mod_Shift, Key_F1 }, [&](auto&) {
  170. auto needle = m_find_textbox->text();
  171. auto substitute = m_replace_textbox->text();
  172. if (needle.is_empty())
  173. return;
  174. auto selection_start = m_editor->normalized_selection().start();
  175. if (!selection_start.is_valid())
  176. selection_start = m_editor->normalized_selection().start();
  177. if (m_find_use_regex)
  178. m_editor->document().update_regex_matches(needle);
  179. auto found_range = m_editor->document().find_previous(needle, selection_start);
  180. if (found_range.is_valid()) {
  181. m_editor->set_selection(found_range);
  182. m_editor->insert_at_cursor_or_replace_selection(substitute);
  183. } else {
  184. GUI::MessageBox::show(window(),
  185. String::formatted("Not found: \"{}\"", needle),
  186. "Not found",
  187. GUI::MessageBox::Type::Information);
  188. }
  189. });
  190. m_replace_all_action = GUI::Action::create("Replace all", { Mod_Ctrl, Key_F2 }, [&](auto&) {
  191. auto needle = m_find_textbox->text();
  192. auto substitute = m_replace_textbox->text();
  193. if (needle.is_empty())
  194. return;
  195. if (m_find_use_regex)
  196. m_editor->document().update_regex_matches(needle);
  197. auto found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  198. while (found_range.is_valid()) {
  199. m_editor->set_selection(found_range);
  200. m_editor->insert_at_cursor_or_replace_selection(substitute);
  201. found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::Yes, m_find_use_regex);
  202. }
  203. });
  204. m_find_previous_button = *find_descendant_of_type_named<GUI::Button>("find_previous_button");
  205. m_find_previous_button->set_action(*m_find_previous_action);
  206. m_find_next_button = *find_descendant_of_type_named<GUI::Button>("find_next_button");
  207. m_find_next_button->set_action(*m_find_next_action);
  208. m_find_textbox->on_return_pressed = [this] {
  209. m_find_next_button->click();
  210. };
  211. m_find_regex_button = m_find_widget->add<GUI::Button>(".*");
  212. m_find_regex_button->set_fixed_width(20);
  213. m_find_regex_button->set_action(*m_find_regex_action);
  214. m_find_textbox->on_escape_pressed = [this] {
  215. m_find_replace_widget->set_visible(false);
  216. m_editor->set_focus(true);
  217. };
  218. m_replace_previous_button = *find_descendant_of_type_named<GUI::Button>("replace_previous_button");
  219. m_replace_previous_button->set_action(*m_replace_previous_action);
  220. m_replace_next_button = *find_descendant_of_type_named<GUI::Button>("replace_next_button");
  221. m_replace_next_button->set_action(*m_replace_next_action);
  222. m_replace_all_button = *find_descendant_of_type_named<GUI::Button>("replace_all_button");
  223. m_replace_all_button->set_action(*m_replace_all_action);
  224. m_replace_textbox->on_return_pressed = [this] {
  225. m_replace_next_button->click();
  226. };
  227. m_replace_textbox->on_escape_pressed = [this] {
  228. m_find_replace_widget->set_visible(false);
  229. m_editor->set_focus(true);
  230. };
  231. m_vim_emulation_setting_action = GUI::Action::create_checkable("Vim emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
  232. if (action.is_checked())
  233. m_editor->set_editing_engine(make<GUI::VimEditingEngine>());
  234. else
  235. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  236. });
  237. m_vim_emulation_setting_action->set_checked(false);
  238. 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&) {
  239. m_find_replace_widget->set_visible(true);
  240. m_find_widget->set_visible(true);
  241. m_replace_widget->set_visible(true);
  242. m_find_textbox->set_focus(true);
  243. if (m_editor->has_selection()) {
  244. auto selected_text = m_editor->document().text_in_range(m_editor->normalized_selection());
  245. m_find_textbox->set_text(selected_text);
  246. }
  247. m_find_textbox->select_all();
  248. });
  249. m_editor->add_custom_context_menu_action(*m_find_replace_action);
  250. m_editor->add_custom_context_menu_action(*m_find_next_action);
  251. m_editor->add_custom_context_menu_action(*m_find_previous_action);
  252. m_statusbar = *find_descendant_of_type_named<GUI::StatusBar>("statusbar");
  253. m_editor->on_cursor_change = [this] { update_statusbar_cursor_position(); };
  254. 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&) {
  255. if (m_document_dirty) {
  256. auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  257. if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
  258. m_save_action->activate();
  259. if (save_document_first_result == GUI::Dialog::ExecResult::ExecCancel)
  260. return;
  261. }
  262. m_document_dirty = false;
  263. m_editor->set_text(StringView());
  264. set_path(LexicalPath());
  265. update_title();
  266. });
  267. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  268. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
  269. if (!open_path.has_value())
  270. return;
  271. if (m_document_dirty) {
  272. auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  273. if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
  274. m_save_action->activate();
  275. if (save_document_first_result == GUI::Dialog::ExecResult::ExecCancel)
  276. return;
  277. }
  278. open_sesame(open_path.value());
  279. });
  280. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  281. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
  282. if (!save_path.has_value())
  283. return;
  284. if (!m_editor->write_to_file(save_path.value())) {
  285. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  286. return;
  287. }
  288. m_document_dirty = false;
  289. set_path(LexicalPath(save_path.value()));
  290. dbgln("Wrote document to {}", save_path.value());
  291. });
  292. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  293. if (!m_path.is_empty()) {
  294. if (!m_editor->write_to_file(m_path)) {
  295. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  296. } else {
  297. m_document_dirty = false;
  298. update_title();
  299. }
  300. return;
  301. }
  302. m_save_as_action->activate();
  303. });
  304. auto menubar = GUI::MenuBar::construct();
  305. auto& app_menu = menubar->add_menu("Text Editor");
  306. app_menu.add_action(*m_new_action);
  307. app_menu.add_action(*m_open_action);
  308. app_menu.add_action(*m_save_action);
  309. app_menu.add_action(*m_save_as_action);
  310. app_menu.add_separator();
  311. app_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  312. if (!request_close())
  313. return;
  314. GUI::Application::the()->quit();
  315. }));
  316. auto& edit_menu = menubar->add_menu("Edit");
  317. edit_menu.add_action(m_editor->undo_action());
  318. edit_menu.add_action(m_editor->redo_action());
  319. edit_menu.add_separator();
  320. edit_menu.add_action(m_editor->cut_action());
  321. edit_menu.add_action(m_editor->copy_action());
  322. edit_menu.add_action(m_editor->paste_action());
  323. edit_menu.add_action(m_editor->delete_action());
  324. edit_menu.add_separator();
  325. edit_menu.add_action(*m_vim_emulation_setting_action);
  326. edit_menu.add_separator();
  327. edit_menu.add_action(*m_find_replace_action);
  328. edit_menu.add_action(*m_find_next_action);
  329. edit_menu.add_action(*m_find_regex_action);
  330. edit_menu.add_action(*m_find_previous_action);
  331. edit_menu.add_action(*m_replace_next_action);
  332. edit_menu.add_action(*m_replace_previous_action);
  333. edit_menu.add_action(*m_replace_all_action);
  334. m_no_preview_action = GUI::Action::create_checkable(
  335. "No preview", [this](auto&) {
  336. set_preview_mode(PreviewMode::None);
  337. });
  338. m_markdown_preview_action = GUI::Action::create_checkable(
  339. "Markdown preview", [this](auto&) {
  340. set_preview_mode(PreviewMode::Markdown);
  341. },
  342. this);
  343. m_html_preview_action = GUI::Action::create_checkable(
  344. "HTML preview", [this](auto&) {
  345. set_preview_mode(PreviewMode::HTML);
  346. },
  347. this);
  348. m_preview_actions.add_action(*m_no_preview_action);
  349. m_preview_actions.add_action(*m_markdown_preview_action);
  350. m_preview_actions.add_action(*m_html_preview_action);
  351. m_preview_actions.set_exclusive(true);
  352. auto& view_menu = menubar->add_menu("View");
  353. view_menu.add_action(GUI::Action::create("Editor font...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"),
  354. [&](auto&) {
  355. auto picker = GUI::FontPicker::construct(window(), &m_editor->font(), true);
  356. if (picker->exec() == GUI::Dialog::ExecOK) {
  357. dbgln("setting font {}", picker->font()->qualified_name());
  358. m_editor->set_font(picker->font());
  359. }
  360. }));
  361. view_menu.add_separator();
  362. m_wrapping_mode_actions.set_exclusive(true);
  363. auto& wrapping_mode_menu = view_menu.add_submenu("Wrapping mode");
  364. m_no_wrapping_action = GUI::Action::create_checkable("No wrapping", [&](auto&) {
  365. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  366. });
  367. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap anywhere", [&](auto&) {
  368. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  369. });
  370. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at words", [&](auto&) {
  371. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  372. });
  373. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  374. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  375. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  376. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  377. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  378. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  379. m_wrap_anywhere_action->set_checked(true);
  380. view_menu.add_separator();
  381. view_menu.add_action(*m_no_preview_action);
  382. view_menu.add_action(*m_markdown_preview_action);
  383. view_menu.add_action(*m_html_preview_action);
  384. view_menu.add_separator();
  385. syntax_actions.set_exclusive(true);
  386. auto& syntax_menu = view_menu.add_submenu("Syntax");
  387. m_plain_text_highlight = GUI::Action::create_checkable("Plain text", [&](auto&) {
  388. m_editor->set_syntax_highlighter({});
  389. m_editor->update();
  390. });
  391. m_plain_text_highlight->set_checked(true);
  392. syntax_actions.add_action(*m_plain_text_highlight);
  393. syntax_menu.add_action(*m_plain_text_highlight);
  394. m_cpp_highlight = GUI::Action::create_checkable("C++", [&](auto&) {
  395. m_editor->set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
  396. m_editor->update();
  397. });
  398. syntax_actions.add_action(*m_cpp_highlight);
  399. syntax_menu.add_action(*m_cpp_highlight);
  400. m_js_highlight = GUI::Action::create_checkable("JavaScript", [&](auto&) {
  401. m_editor->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  402. m_editor->update();
  403. });
  404. syntax_actions.add_action(*m_js_highlight);
  405. syntax_menu.add_action(*m_js_highlight);
  406. m_gml_highlight = GUI::Action::create_checkable("GML", [&](auto&) {
  407. m_editor->set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
  408. m_editor->update();
  409. });
  410. syntax_actions.add_action(*m_gml_highlight);
  411. syntax_menu.add_action(*m_gml_highlight);
  412. m_ini_highlight = GUI::Action::create_checkable("INI File", [&](auto&) {
  413. m_editor->set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  414. m_editor->update();
  415. });
  416. syntax_actions.add_action(*m_ini_highlight);
  417. syntax_menu.add_action(*m_ini_highlight);
  418. m_shell_highlight = GUI::Action::create_checkable("Shell File", [&](auto&) {
  419. m_editor->set_syntax_highlighter(make<GUI::ShellSyntaxHighlighter>());
  420. m_editor->update();
  421. });
  422. syntax_actions.add_action(*m_shell_highlight);
  423. syntax_menu.add_action(*m_shell_highlight);
  424. auto& help_menu = menubar->add_menu("Help");
  425. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  426. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/TextEditor.md"), "/bin/Help");
  427. }));
  428. help_menu.add_action(GUI::CommonActions::make_about_action("Text Editor", GUI::Icon::default_icon("app-text-editor"), window()));
  429. GUI::Application::the()->set_menubar(move(menubar));
  430. toolbar.add_action(*m_new_action);
  431. toolbar.add_action(*m_open_action);
  432. toolbar.add_action(*m_save_action);
  433. toolbar.add_separator();
  434. toolbar.add_action(m_editor->cut_action());
  435. toolbar.add_action(m_editor->copy_action());
  436. toolbar.add_action(m_editor->paste_action());
  437. toolbar.add_action(m_editor->delete_action());
  438. toolbar.add_separator();
  439. toolbar.add_action(m_editor->undo_action());
  440. toolbar.add_action(m_editor->redo_action());
  441. }
  442. TextEditorWidget::~TextEditorWidget()
  443. {
  444. }
  445. void TextEditorWidget::set_path(const LexicalPath& lexical_path)
  446. {
  447. m_path = lexical_path.string();
  448. m_name = lexical_path.title();
  449. m_extension = lexical_path.extension();
  450. if (m_extension == "c" || m_extension == "cc" || m_extension == "cxx" || m_extension == "cpp" || m_extension == "h") {
  451. m_cpp_highlight->activate();
  452. } else if (m_extension == "js" || m_extension == "json") {
  453. m_js_highlight->activate();
  454. } else if (m_extension == "gml") {
  455. m_gml_highlight->activate();
  456. } else if (m_extension == "ini") {
  457. m_ini_highlight->activate();
  458. } else {
  459. m_plain_text_highlight->activate();
  460. }
  461. if (m_auto_detect_preview_mode) {
  462. if (m_extension == "md")
  463. set_preview_mode(PreviewMode::Markdown);
  464. else if (m_extension == "html")
  465. set_preview_mode(PreviewMode::HTML);
  466. else
  467. set_preview_mode(PreviewMode::None);
  468. }
  469. update_title();
  470. }
  471. void TextEditorWidget::update_title()
  472. {
  473. StringBuilder builder;
  474. if (m_path.is_empty())
  475. builder.append("Untitled");
  476. else
  477. builder.append(m_path);
  478. if (m_document_dirty)
  479. builder.append(" (*)");
  480. builder.append(" - Text Editor");
  481. window()->set_title(builder.to_string());
  482. }
  483. void TextEditorWidget::open_sesame(const String& path)
  484. {
  485. auto file = Core::File::construct(path);
  486. if (!file->open(Core::IODevice::ReadOnly) && file->error() != ENOENT) {
  487. GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  488. return;
  489. }
  490. m_editor->set_text(file->read_all());
  491. m_document_dirty = false;
  492. m_document_opening = true;
  493. set_path(LexicalPath(path));
  494. m_editor->set_focus(true);
  495. }
  496. bool TextEditorWidget::request_close()
  497. {
  498. if (!m_document_dirty)
  499. return true;
  500. 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);
  501. if (result == GUI::MessageBox::ExecYes) {
  502. m_save_action->activate();
  503. return true;
  504. }
  505. if (result == GUI::MessageBox::ExecNo)
  506. return true;
  507. return false;
  508. }
  509. void TextEditorWidget::drop_event(GUI::DropEvent& event)
  510. {
  511. event.accept();
  512. window()->move_to_front();
  513. if (event.mime_data().has_urls()) {
  514. auto urls = event.mime_data().urls();
  515. if (urls.is_empty())
  516. return;
  517. if (urls.size() > 1) {
  518. GUI::MessageBox::show(window(), "TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error);
  519. return;
  520. }
  521. open_sesame(urls.first().path());
  522. }
  523. }
  524. void TextEditorWidget::set_preview_mode(PreviewMode mode)
  525. {
  526. if (m_preview_mode == mode)
  527. return;
  528. m_preview_mode = mode;
  529. if (m_preview_mode == PreviewMode::HTML) {
  530. m_html_preview_action->set_checked(true);
  531. m_page_view->set_visible(true);
  532. update_html_preview();
  533. } else if (m_preview_mode == PreviewMode::Markdown) {
  534. m_markdown_preview_action->set_checked(true);
  535. m_page_view->set_visible(true);
  536. update_markdown_preview();
  537. } else {
  538. m_no_preview_action->set_checked(true);
  539. m_page_view->set_visible(false);
  540. }
  541. }
  542. void TextEditorWidget::update_preview()
  543. {
  544. switch (m_preview_mode) {
  545. case PreviewMode::Markdown:
  546. update_markdown_preview();
  547. break;
  548. case PreviewMode::HTML:
  549. update_html_preview();
  550. break;
  551. default:
  552. break;
  553. }
  554. }
  555. void TextEditorWidget::update_markdown_preview()
  556. {
  557. auto document = Markdown::Document::parse(m_editor->text());
  558. if (document) {
  559. auto html = document->render_to_html();
  560. auto current_scroll_pos = m_page_view->visible_content_rect();
  561. m_page_view->load_html(html, URL::create_with_file_protocol(m_path));
  562. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  563. }
  564. }
  565. void TextEditorWidget::update_html_preview()
  566. {
  567. auto current_scroll_pos = m_page_view->visible_content_rect();
  568. m_page_view->load_html(m_editor->text(), URL::create_with_file_protocol(m_path));
  569. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  570. }
  571. void TextEditorWidget::update_statusbar_cursor_position()
  572. {
  573. StringBuilder builder;
  574. builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
  575. m_statusbar->set_text(builder.to_string());
  576. }