TextEditorWidget.cpp 23 KB

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