TextEditorWidget.cpp 23 KB

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