TextEditorWidget.cpp 24 KB

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