MainWidget.cpp 30 KB

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