MainWidget.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MainWidget.h"
  7. #include <AK/JsonValue.h>
  8. #include <AK/Optional.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/URL.h>
  11. #include <Applications/TextEditor/TextEditorWindowGML.h>
  12. #include <LibCore/ConfigFile.h>
  13. #include <LibCore/File.h>
  14. #include <LibCpp/SyntaxHighlighter.h>
  15. #include <LibDesktop/Launcher.h>
  16. #include <LibGUI/Action.h>
  17. #include <LibGUI/BoxLayout.h>
  18. #include <LibGUI/Button.h>
  19. #include <LibGUI/CheckBox.h>
  20. #include <LibGUI/FilePicker.h>
  21. #include <LibGUI/FontPicker.h>
  22. #include <LibGUI/GMLSyntaxHighlighter.h>
  23. #include <LibGUI/GroupBox.h>
  24. #include <LibGUI/INISyntaxHighlighter.h>
  25. #include <LibGUI/Menu.h>
  26. #include <LibGUI/Menubar.h>
  27. #include <LibGUI/MessageBox.h>
  28. #include <LibGUI/RegularEditingEngine.h>
  29. #include <LibGUI/Splitter.h>
  30. #include <LibGUI/Statusbar.h>
  31. #include <LibGUI/TextBox.h>
  32. #include <LibGUI/TextEditor.h>
  33. #include <LibGUI/Toolbar.h>
  34. #include <LibGUI/ToolbarContainer.h>
  35. #include <LibGUI/VimEditingEngine.h>
  36. #include <LibGfx/Font.h>
  37. #include <LibGfx/Painter.h>
  38. #include <LibJS/SyntaxHighlighter.h>
  39. #include <LibMarkdown/Document.h>
  40. #include <LibSQL/SyntaxHighlighter.h>
  41. #include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
  42. #include <LibWeb/OutOfProcessWebView.h>
  43. #include <Shell/SyntaxHighlighter.h>
  44. namespace TextEditor {
  45. MainWidget::MainWidget()
  46. {
  47. load_from_gml(text_editor_window_gml);
  48. m_config = Core::ConfigFile::get_for_app("TextEditor");
  49. m_toolbar = *find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  50. m_toolbar_container = *find_descendant_of_type_named<GUI::ToolbarContainer>("toolbar_container");
  51. m_editor = *find_descendant_of_type_named<GUI::TextEditor>("editor");
  52. m_editor->set_ruler_visible(true);
  53. m_editor->set_automatic_indentation_enabled(true);
  54. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  55. m_editor->on_change = [this] {
  56. update_preview();
  57. };
  58. m_editor->on_modified_change = [this](bool modified) {
  59. window()->set_modified(modified);
  60. };
  61. m_find_replace_widget = *find_descendant_of_type_named<GUI::GroupBox>("find_replace_widget");
  62. m_find_widget = *find_descendant_of_type_named<GUI::Widget>("find_widget");
  63. m_replace_widget = *find_descendant_of_type_named<GUI::Widget>("replace_widget");
  64. m_find_textbox = *find_descendant_of_type_named<GUI::TextBox>("find_textbox");
  65. m_find_textbox->set_placeholder("Find");
  66. m_replace_textbox = *find_descendant_of_type_named<GUI::TextBox>("replace_textbox");
  67. m_replace_textbox->set_placeholder("Replace");
  68. m_match_case_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("match_case_checkbox");
  69. m_match_case_checkbox->on_checked = [this] {
  70. m_match_case = m_match_case_checkbox->is_checked();
  71. };
  72. m_match_case_checkbox->set_checked(true);
  73. m_regex_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("regex_checkbox");
  74. m_regex_checkbox->on_checked = [this] {
  75. m_use_regex = m_regex_checkbox->is_checked();
  76. };
  77. m_regex_checkbox->set_checked(false);
  78. m_wrap_around_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("wrap_around_checkbox");
  79. m_wrap_around_checkbox->on_checked = [this] {
  80. m_should_wrap = m_wrap_around_checkbox->is_checked();
  81. };
  82. m_wrap_around_checkbox->set_checked(true);
  83. 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&) {
  84. auto needle = m_find_textbox->text();
  85. if (needle.is_empty())
  86. return;
  87. if (m_use_regex)
  88. m_editor->document().update_regex_matches(needle);
  89. auto found_range = m_editor->document().find_next(needle, m_editor->normalized_selection().end(), m_should_wrap ? GUI::TextDocument::SearchShouldWrap::Yes : GUI::TextDocument::SearchShouldWrap::No, m_use_regex, m_match_case);
  90. dbgln("find_next('{}') returned {}", needle, found_range);
  91. if (found_range.is_valid()) {
  92. m_editor->set_selection(found_range);
  93. } else {
  94. GUI::MessageBox::show(window(),
  95. String::formatted("Not found: \"{}\"", needle),
  96. "Not found",
  97. GUI::MessageBox::Type::Information);
  98. }
  99. });
  100. m_find_previous_action = GUI::Action::create("Find Pr&evious", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find-previous.png"), [&](auto&) {
  101. auto needle = m_find_textbox->text();
  102. if (needle.is_empty())
  103. return;
  104. if (m_use_regex)
  105. m_editor->document().update_regex_matches(needle);
  106. auto selection_start = m_editor->normalized_selection().start();
  107. if (!selection_start.is_valid())
  108. selection_start = m_editor->normalized_selection().end();
  109. auto found_range = m_editor->document().find_previous(needle, selection_start, m_should_wrap ? GUI::TextDocument::SearchShouldWrap::Yes : GUI::TextDocument::SearchShouldWrap::No, m_use_regex, m_match_case);
  110. dbgln("find_prev(\"{}\") returned {}", needle, found_range);
  111. if (found_range.is_valid()) {
  112. m_editor->set_selection(found_range);
  113. } else {
  114. GUI::MessageBox::show(window(),
  115. String::formatted("Not found: \"{}\"", needle),
  116. "Not found",
  117. GUI::MessageBox::Type::Information);
  118. }
  119. });
  120. m_replace_action = GUI::Action::create("Rep&lace", { Mod_Ctrl, Key_F1 }, [&](auto&) {
  121. auto needle = m_find_textbox->text();
  122. auto substitute = m_replace_textbox->text();
  123. if (needle.is_empty())
  124. return;
  125. if (m_use_regex)
  126. m_editor->document().update_regex_matches(needle);
  127. auto found_range = m_editor->document().find_next(needle, m_editor->normalized_selection().start(), m_should_wrap ? GUI::TextDocument::SearchShouldWrap::Yes : GUI::TextDocument::SearchShouldWrap::No, m_use_regex, m_match_case);
  128. if (found_range.is_valid()) {
  129. m_editor->set_selection(found_range);
  130. m_editor->insert_at_cursor_or_replace_selection(substitute);
  131. } else {
  132. GUI::MessageBox::show(window(),
  133. String::formatted("Not found: \"{}\"", needle),
  134. "Not found",
  135. GUI::MessageBox::Type::Information);
  136. }
  137. });
  138. m_replace_all_action = GUI::Action::create("Replace &All", { Mod_Ctrl, Key_F2 }, [&](auto&) {
  139. auto needle = m_find_textbox->text();
  140. auto substitute = m_replace_textbox->text();
  141. if (needle.is_empty())
  142. return;
  143. if (m_use_regex)
  144. m_editor->document().update_regex_matches(needle);
  145. auto found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::Yes, m_use_regex, m_match_case);
  146. while (found_range.is_valid()) {
  147. m_editor->set_selection(found_range);
  148. m_editor->insert_at_cursor_or_replace_selection(substitute);
  149. found_range = m_editor->document().find_next(needle, {}, GUI::TextDocument::SearchShouldWrap::Yes, m_use_regex, m_match_case);
  150. }
  151. });
  152. m_find_previous_button = *find_descendant_of_type_named<GUI::Button>("find_previous_button");
  153. m_find_previous_button->set_action(*m_find_previous_action);
  154. m_find_previous_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/find-previous.png"));
  155. m_find_next_button = *find_descendant_of_type_named<GUI::Button>("find_next_button");
  156. m_find_next_button->set_action(*m_find_next_action);
  157. m_find_next_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/find-next.png"));
  158. m_find_textbox->on_return_pressed = [this] {
  159. m_find_next_button->click();
  160. };
  161. m_find_textbox->on_escape_pressed = [this] {
  162. m_find_replace_widget->set_visible(false);
  163. m_editor->set_focus(true);
  164. };
  165. m_replace_button = *find_descendant_of_type_named<GUI::Button>("replace_button");
  166. m_replace_button->set_action(*m_replace_action);
  167. m_replace_all_button = *find_descendant_of_type_named<GUI::Button>("replace_all_button");
  168. m_replace_all_button->set_action(*m_replace_all_action);
  169. m_replace_textbox->on_return_pressed = [this] {
  170. m_replace_button->click();
  171. };
  172. m_replace_textbox->on_escape_pressed = [this] {
  173. m_find_replace_widget->set_visible(false);
  174. m_editor->set_focus(true);
  175. };
  176. m_vim_emulation_setting_action = GUI::Action::create_checkable("&Vim Emulation", { Mod_Ctrl | Mod_Shift | Mod_Alt, Key_V }, [&](auto& action) {
  177. if (action.is_checked())
  178. m_editor->set_editing_engine(make<GUI::VimEditingEngine>());
  179. else
  180. m_editor->set_editing_engine(make<GUI::RegularEditingEngine>());
  181. });
  182. m_vim_emulation_setting_action->set_checked(false);
  183. 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&) {
  184. m_find_replace_widget->set_visible(true);
  185. m_find_widget->set_visible(true);
  186. m_replace_widget->set_visible(true);
  187. m_find_textbox->set_focus(true);
  188. if (m_editor->has_selection()) {
  189. auto selected_text = m_editor->document().text_in_range(m_editor->normalized_selection());
  190. m_find_textbox->set_text(selected_text);
  191. }
  192. m_find_textbox->select_all();
  193. });
  194. m_editor->add_custom_context_menu_action(*m_find_replace_action);
  195. m_editor->add_custom_context_menu_action(*m_find_next_action);
  196. m_editor->add_custom_context_menu_action(*m_find_previous_action);
  197. m_statusbar = *find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  198. GUI::Application::the()->on_action_enter = [this](GUI::Action& action) {
  199. auto text = action.status_tip();
  200. if (text.is_empty())
  201. text = Gfx::parse_ampersand_string(action.text());
  202. m_statusbar->set_override_text(move(text));
  203. };
  204. GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
  205. m_statusbar->set_override_text({});
  206. };
  207. m_editor->on_cursor_change = [this] { update_statusbar(); };
  208. m_editor->on_selection_change = [this] { update_statusbar(); };
  209. 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&) {
  210. if (editor().document().is_modified()) {
  211. auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  212. if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
  213. m_save_action->activate();
  214. if (save_document_first_result == GUI::Dialog::ExecResult::ExecCancel)
  215. return;
  216. }
  217. m_editor->set_text(StringView());
  218. set_path(LexicalPath());
  219. update_title();
  220. });
  221. m_open_action = GUI::CommonActions::make_open_action([this](auto&) {
  222. Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
  223. if (!open_path.has_value())
  224. return;
  225. if (editor().document().is_modified()) {
  226. auto save_document_first_result = GUI::MessageBox::show(window(), "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  227. if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
  228. m_save_action->activate();
  229. if (save_document_first_result == GUI::Dialog::ExecResult::ExecCancel)
  230. return;
  231. }
  232. open_file(open_path.value());
  233. });
  234. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  235. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), m_name.is_null() ? "Untitled" : m_name, m_extension.is_null() ? "txt" : m_extension);
  236. if (!save_path.has_value())
  237. return;
  238. if (!m_editor->write_to_file(save_path.value())) {
  239. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  240. return;
  241. }
  242. // FIXME: It would be cool if this would propagate from GUI::TextDocument somehow.
  243. window()->set_modified(false);
  244. set_path(LexicalPath(save_path.value()));
  245. dbgln("Wrote document to {}", save_path.value());
  246. });
  247. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  248. if (!m_path.is_empty()) {
  249. if (!m_editor->write_to_file(m_path)) {
  250. GUI::MessageBox::show(window(), "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
  251. } else {
  252. // FIXME: It would be cool if this would propagate from GUI::TextDocument somehow.
  253. window()->set_modified(false);
  254. update_title();
  255. }
  256. return;
  257. }
  258. m_save_as_action->activate();
  259. });
  260. m_toolbar->add_action(*m_new_action);
  261. m_toolbar->add_action(*m_open_action);
  262. m_toolbar->add_action(*m_save_action);
  263. m_toolbar->add_separator();
  264. m_toolbar->add_action(m_editor->cut_action());
  265. m_toolbar->add_action(m_editor->copy_action());
  266. m_toolbar->add_action(m_editor->paste_action());
  267. m_toolbar->add_action(m_editor->delete_action());
  268. m_toolbar->add_separator();
  269. m_toolbar->add_action(m_editor->undo_action());
  270. m_toolbar->add_action(m_editor->redo_action());
  271. }
  272. MainWidget::~MainWidget()
  273. {
  274. }
  275. Web::OutOfProcessWebView& MainWidget::ensure_web_view()
  276. {
  277. if (!m_page_view) {
  278. auto& web_view_container = *find_descendant_of_type_named<GUI::Widget>("web_view_container");
  279. m_page_view = web_view_container.add<Web::OutOfProcessWebView>();
  280. m_page_view->on_link_hover = [this](auto& url) {
  281. if (url.is_valid())
  282. m_statusbar->set_text(url.to_string());
  283. else
  284. update_statusbar();
  285. };
  286. m_page_view->on_link_click = [&](auto& url, auto&, unsigned) {
  287. if (!Desktop::Launcher::open(url)) {
  288. GUI::MessageBox::show(
  289. window(),
  290. String::formatted("The link to '{}' could not be opened.", url),
  291. "Failed to open link",
  292. GUI::MessageBox::Type::Error);
  293. }
  294. };
  295. }
  296. return *m_page_view;
  297. }
  298. void MainWidget::initialize_menubar(GUI::Menubar& menubar)
  299. {
  300. auto& file_menu = menubar.add_menu("&File");
  301. file_menu.add_action(*m_new_action);
  302. file_menu.add_action(*m_open_action);
  303. file_menu.add_action(*m_save_action);
  304. file_menu.add_action(*m_save_as_action);
  305. file_menu.add_separator();
  306. file_menu.add_action(GUI::CommonActions::make_quit_action([this](auto&) {
  307. if (!request_close())
  308. return;
  309. GUI::Application::the()->quit();
  310. }));
  311. auto& edit_menu = menubar.add_menu("&Edit");
  312. edit_menu.add_action(m_editor->undo_action());
  313. edit_menu.add_action(m_editor->redo_action());
  314. edit_menu.add_separator();
  315. edit_menu.add_action(m_editor->cut_action());
  316. edit_menu.add_action(m_editor->copy_action());
  317. edit_menu.add_action(m_editor->paste_action());
  318. edit_menu.add_action(m_editor->delete_action());
  319. edit_menu.add_separator();
  320. edit_menu.add_action(*m_vim_emulation_setting_action);
  321. edit_menu.add_separator();
  322. edit_menu.add_action(*m_find_replace_action);
  323. edit_menu.add_action(*m_find_next_action);
  324. edit_menu.add_action(*m_find_previous_action);
  325. edit_menu.add_action(*m_replace_action);
  326. edit_menu.add_action(*m_replace_all_action);
  327. m_no_preview_action = GUI::Action::create_checkable(
  328. "&No Preview", [this](auto&) {
  329. set_preview_mode(PreviewMode::None);
  330. });
  331. m_markdown_preview_action = GUI::Action::create_checkable(
  332. "&Markdown Preview", [this](auto&) {
  333. set_preview_mode(PreviewMode::Markdown);
  334. },
  335. this);
  336. m_html_preview_action = GUI::Action::create_checkable(
  337. "&HTML Preview", [this](auto&) {
  338. set_preview_mode(PreviewMode::HTML);
  339. },
  340. this);
  341. m_preview_actions.add_action(*m_no_preview_action);
  342. m_preview_actions.add_action(*m_markdown_preview_action);
  343. m_preview_actions.add_action(*m_html_preview_action);
  344. m_preview_actions.set_exclusive(true);
  345. m_layout_toolbar_action = GUI::Action::create_checkable("&Toolbar", [&](auto& action) {
  346. action.is_checked() ? m_toolbar_container->set_visible(true) : m_toolbar_container->set_visible(false);
  347. m_config->write_bool_entry("Layout", "ShowToolbar", action.is_checked());
  348. m_config->sync();
  349. });
  350. auto show_toolbar = m_config->read_bool_entry("Layout", "ShowToolbar", true);
  351. m_layout_toolbar_action->set_checked(show_toolbar);
  352. m_toolbar_container->set_visible(show_toolbar);
  353. m_layout_statusbar_action = GUI::Action::create_checkable("&Status Bar", [&](auto& action) {
  354. action.is_checked() ? m_statusbar->set_visible(true) : m_statusbar->set_visible(false);
  355. m_config->write_bool_entry("Layout", "ShowStatusbar", action.is_checked());
  356. m_config->sync();
  357. });
  358. auto show_statusbar = m_config->read_bool_entry("Layout", "ShowStatusbar", true);
  359. m_layout_statusbar_action->set_checked(show_statusbar);
  360. m_statusbar->set_visible(show_statusbar);
  361. m_layout_ruler_action = GUI::Action::create_checkable("Ruler", [&](auto& action) {
  362. action.is_checked() ? m_editor->set_ruler_visible(true) : m_editor->set_ruler_visible(false);
  363. m_config->write_bool_entry("Layout", "ShowRuler", action.is_checked());
  364. m_config->sync();
  365. });
  366. auto show_ruler = m_config->read_bool_entry("Layout", "ShowRuler", true);
  367. m_layout_ruler_action->set_checked(show_ruler);
  368. m_editor->set_ruler_visible(show_ruler);
  369. auto& view_menu = menubar.add_menu("&View");
  370. auto& layout_menu = view_menu.add_submenu("&Layout");
  371. layout_menu.add_action(*m_layout_toolbar_action);
  372. layout_menu.add_action(*m_layout_statusbar_action);
  373. layout_menu.add_action(*m_layout_ruler_action);
  374. view_menu.add_separator();
  375. view_menu.add_action(GUI::Action::create("Editor &Font...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-font-editor.png"),
  376. [&](auto&) {
  377. auto picker = GUI::FontPicker::construct(window(), &m_editor->font(), false);
  378. if (picker->exec() == GUI::Dialog::ExecOK) {
  379. dbgln("setting font {}", picker->font()->qualified_name());
  380. m_editor->set_font(picker->font());
  381. }
  382. }));
  383. view_menu.add_separator();
  384. m_wrapping_mode_actions.set_exclusive(true);
  385. auto& wrapping_mode_menu = view_menu.add_submenu("&Wrapping Mode");
  386. m_no_wrapping_action = GUI::Action::create_checkable("&No Wrapping", [&](auto&) {
  387. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap);
  388. });
  389. m_wrap_anywhere_action = GUI::Action::create_checkable("Wrap &Anywhere", [&](auto&) {
  390. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAnywhere);
  391. });
  392. m_wrap_at_words_action = GUI::Action::create_checkable("Wrap at &Words", [&](auto&) {
  393. m_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::WrapAtWords);
  394. });
  395. m_wrapping_mode_actions.add_action(*m_no_wrapping_action);
  396. m_wrapping_mode_actions.add_action(*m_wrap_anywhere_action);
  397. m_wrapping_mode_actions.add_action(*m_wrap_at_words_action);
  398. wrapping_mode_menu.add_action(*m_no_wrapping_action);
  399. wrapping_mode_menu.add_action(*m_wrap_anywhere_action);
  400. wrapping_mode_menu.add_action(*m_wrap_at_words_action);
  401. m_no_wrapping_action->set_checked(true);
  402. view_menu.add_separator();
  403. m_soft_tab_width_actions.set_exclusive(true);
  404. auto& soft_tab_width_menu = view_menu.add_submenu("&Tab Width");
  405. m_soft_tab_1_width_action = GUI::Action::create_checkable("1", [&](auto&) {
  406. m_editor->set_soft_tab_width(1);
  407. });
  408. m_soft_tab_2_width_action = GUI::Action::create_checkable("2", [&](auto&) {
  409. m_editor->set_soft_tab_width(2);
  410. });
  411. m_soft_tab_4_width_action = GUI::Action::create_checkable("4", [&](auto&) {
  412. m_editor->set_soft_tab_width(4);
  413. });
  414. m_soft_tab_8_width_action = GUI::Action::create_checkable("8", [&](auto&) {
  415. m_editor->set_soft_tab_width(8);
  416. });
  417. m_soft_tab_16_width_action = GUI::Action::create_checkable("16", [&](auto&) {
  418. m_editor->set_soft_tab_width(16);
  419. });
  420. m_soft_tab_width_actions.add_action(*m_soft_tab_1_width_action);
  421. m_soft_tab_width_actions.add_action(*m_soft_tab_2_width_action);
  422. m_soft_tab_width_actions.add_action(*m_soft_tab_4_width_action);
  423. m_soft_tab_width_actions.add_action(*m_soft_tab_8_width_action);
  424. m_soft_tab_width_actions.add_action(*m_soft_tab_16_width_action);
  425. soft_tab_width_menu.add_action(*m_soft_tab_1_width_action);
  426. soft_tab_width_menu.add_action(*m_soft_tab_2_width_action);
  427. soft_tab_width_menu.add_action(*m_soft_tab_4_width_action);
  428. soft_tab_width_menu.add_action(*m_soft_tab_8_width_action);
  429. soft_tab_width_menu.add_action(*m_soft_tab_16_width_action);
  430. m_soft_tab_4_width_action->set_checked(true);
  431. view_menu.add_separator();
  432. m_visualize_trailing_whitespace_action = GUI::Action::create_checkable("&Visualize Trailing Whitespace", [&](auto&) {
  433. m_editor->set_visualize_trailing_whitespace(m_visualize_trailing_whitespace_action->is_checked());
  434. });
  435. m_visualize_leading_whitespace_action = GUI::Action::create_checkable("Visualize &Leading Whitespace", [&](auto&) {
  436. m_editor->set_visualize_leading_whitespace(m_visualize_leading_whitespace_action->is_checked());
  437. });
  438. m_visualize_trailing_whitespace_action->set_checked(true);
  439. view_menu.add_action(*m_visualize_trailing_whitespace_action);
  440. view_menu.add_action(*m_visualize_leading_whitespace_action);
  441. view_menu.add_separator();
  442. view_menu.add_action(*m_no_preview_action);
  443. view_menu.add_action(*m_markdown_preview_action);
  444. view_menu.add_action(*m_html_preview_action);
  445. m_no_preview_action->set_checked(true);
  446. view_menu.add_separator();
  447. syntax_actions.set_exclusive(true);
  448. auto& syntax_menu = view_menu.add_submenu("&Syntax");
  449. m_plain_text_highlight = GUI::Action::create_checkable("&Plain Text", [&](auto&) {
  450. m_editor->set_syntax_highlighter({});
  451. m_editor->update();
  452. });
  453. m_plain_text_highlight->set_checked(true);
  454. syntax_actions.add_action(*m_plain_text_highlight);
  455. syntax_menu.add_action(*m_plain_text_highlight);
  456. m_cpp_highlight = GUI::Action::create_checkable("&C++", [&](auto&) {
  457. m_editor->set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
  458. m_editor->update();
  459. });
  460. syntax_actions.add_action(*m_cpp_highlight);
  461. syntax_menu.add_action(*m_cpp_highlight);
  462. m_js_highlight = GUI::Action::create_checkable("&JavaScript", [&](auto&) {
  463. m_editor->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  464. m_editor->update();
  465. });
  466. syntax_actions.add_action(*m_js_highlight);
  467. syntax_menu.add_action(*m_js_highlight);
  468. m_html_highlight = GUI::Action::create_checkable("&HTML File", [&](auto&) {
  469. m_editor->set_syntax_highlighter(make<Web::HTML::SyntaxHighlighter>());
  470. m_editor->update();
  471. });
  472. syntax_actions.add_action(*m_html_highlight);
  473. syntax_menu.add_action(*m_html_highlight);
  474. m_gml_highlight = GUI::Action::create_checkable("&GML", [&](auto&) {
  475. m_editor->set_syntax_highlighter(make<GUI::GMLSyntaxHighlighter>());
  476. m_editor->update();
  477. });
  478. syntax_actions.add_action(*m_gml_highlight);
  479. syntax_menu.add_action(*m_gml_highlight);
  480. m_ini_highlight = GUI::Action::create_checkable("&INI File", [&](auto&) {
  481. m_editor->set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
  482. m_editor->update();
  483. });
  484. syntax_actions.add_action(*m_ini_highlight);
  485. syntax_menu.add_action(*m_ini_highlight);
  486. m_shell_highlight = GUI::Action::create_checkable("&Shell File", [&](auto&) {
  487. m_editor->set_syntax_highlighter(make<Shell::SyntaxHighlighter>());
  488. m_editor->update();
  489. });
  490. syntax_actions.add_action(*m_shell_highlight);
  491. syntax_menu.add_action(*m_shell_highlight);
  492. m_sql_highlight = GUI::Action::create_checkable("S&QL File", [&](auto&) {
  493. m_editor->set_syntax_highlighter(make<SQL::SyntaxHighlighter>());
  494. m_editor->update();
  495. });
  496. syntax_actions.add_action(*m_sql_highlight);
  497. syntax_menu.add_action(*m_sql_highlight);
  498. auto& help_menu = menubar.add_menu("&Help");
  499. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  500. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/TextEditor.md"), "/bin/Help");
  501. }));
  502. help_menu.add_action(GUI::CommonActions::make_about_action("Text Editor", GUI::Icon::default_icon("app-text-editor"), window()));
  503. }
  504. void MainWidget::set_path(const LexicalPath& lexical_path)
  505. {
  506. m_path = lexical_path.string();
  507. m_name = lexical_path.title();
  508. m_extension = lexical_path.extension();
  509. if (m_extension == "c" || m_extension == "cc" || m_extension == "cxx" || m_extension == "cpp" || m_extension == "h") {
  510. m_cpp_highlight->activate();
  511. } else if (m_extension == "js" || m_extension == "json") {
  512. m_js_highlight->activate();
  513. } else if (m_extension == "gml") {
  514. m_gml_highlight->activate();
  515. } else if (m_extension == "ini") {
  516. m_ini_highlight->activate();
  517. } else if (m_extension == "sql") {
  518. m_sql_highlight->activate();
  519. } else if (m_extension == "html") {
  520. m_html_highlight->activate();
  521. } else {
  522. m_plain_text_highlight->activate();
  523. }
  524. if (m_auto_detect_preview_mode) {
  525. if (m_extension == "md")
  526. set_preview_mode(PreviewMode::Markdown);
  527. else if (m_extension == "html")
  528. set_preview_mode(PreviewMode::HTML);
  529. else
  530. set_preview_mode(PreviewMode::None);
  531. }
  532. update_title();
  533. }
  534. void MainWidget::update_title()
  535. {
  536. StringBuilder builder;
  537. if (m_path.is_empty())
  538. builder.append("Untitled");
  539. else
  540. builder.append(m_path);
  541. builder.append("[*] - Text Editor");
  542. window()->set_title(builder.to_string());
  543. }
  544. bool MainWidget::open_file(const String& path)
  545. {
  546. auto file = Core::File::construct(path);
  547. if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) {
  548. GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  549. return false;
  550. }
  551. if (file->is_device()) {
  552. GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error);
  553. return false;
  554. }
  555. m_editor->set_text(file->read_all());
  556. set_path(LexicalPath(path));
  557. m_editor->set_focus(true);
  558. return true;
  559. }
  560. bool MainWidget::request_close()
  561. {
  562. if (!editor().document().is_modified())
  563. return true;
  564. 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);
  565. if (result == GUI::MessageBox::ExecYes) {
  566. m_save_action->activate();
  567. return true;
  568. }
  569. if (result == GUI::MessageBox::ExecNo)
  570. return true;
  571. return false;
  572. }
  573. void MainWidget::drop_event(GUI::DropEvent& event)
  574. {
  575. event.accept();
  576. window()->move_to_front();
  577. if (event.mime_data().has_urls()) {
  578. auto urls = event.mime_data().urls();
  579. if (urls.is_empty())
  580. return;
  581. if (urls.size() > 1) {
  582. GUI::MessageBox::show(window(), "TextEditor can only open one file at a time!", "One at a time please!", GUI::MessageBox::Type::Error);
  583. return;
  584. }
  585. open_file(urls.first().path());
  586. }
  587. }
  588. void MainWidget::set_web_view_visible(bool visible)
  589. {
  590. if (!visible && !m_page_view)
  591. return;
  592. ensure_web_view();
  593. auto& web_view_container = *find_descendant_of_type_named<GUI::Widget>("web_view_container");
  594. web_view_container.set_visible(visible);
  595. }
  596. void MainWidget::set_preview_mode(PreviewMode mode)
  597. {
  598. if (m_preview_mode == mode)
  599. return;
  600. m_preview_mode = mode;
  601. if (m_preview_mode == PreviewMode::HTML) {
  602. m_html_preview_action->set_checked(true);
  603. set_web_view_visible(true);
  604. update_html_preview();
  605. } else if (m_preview_mode == PreviewMode::Markdown) {
  606. m_markdown_preview_action->set_checked(true);
  607. set_web_view_visible(true);
  608. update_markdown_preview();
  609. } else {
  610. m_no_preview_action->set_checked(true);
  611. set_web_view_visible(false);
  612. }
  613. }
  614. void MainWidget::update_preview()
  615. {
  616. switch (m_preview_mode) {
  617. case PreviewMode::Markdown:
  618. update_markdown_preview();
  619. break;
  620. case PreviewMode::HTML:
  621. update_html_preview();
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. void MainWidget::update_markdown_preview()
  628. {
  629. auto document = Markdown::Document::parse(m_editor->text());
  630. if (document) {
  631. auto html = document->render_to_html();
  632. auto current_scroll_pos = m_page_view->visible_content_rect();
  633. m_page_view->load_html(html, URL::create_with_file_protocol(m_path));
  634. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  635. }
  636. }
  637. void MainWidget::update_html_preview()
  638. {
  639. auto current_scroll_pos = m_page_view->visible_content_rect();
  640. m_page_view->load_html(m_editor->text(), URL::create_with_file_protocol(m_path));
  641. m_page_view->scroll_into_view(current_scroll_pos, true, true);
  642. }
  643. void MainWidget::update_statusbar()
  644. {
  645. StringBuilder builder;
  646. builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
  647. if (m_editor->has_selection()) {
  648. String selected_text = m_editor->selected_text();
  649. auto word_count = m_editor->number_of_selected_words();
  650. builder.appendff(" Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
  651. }
  652. m_statusbar->set_text(builder.to_string());
  653. }
  654. }