TextEditorWidget.cpp 23 KB

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