TextEditorWidget.cpp 24 KB

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