TextEditorWidget.cpp 23 KB

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