main.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "History.h"
  27. #include "InspectorWidget.h"
  28. #include <LibCore/CFile.h>
  29. #include <LibGUI/GAboutDialog.h>
  30. #include <LibGUI/GAction.h>
  31. #include <LibGUI/GApplication.h>
  32. #include <LibGUI/GBoxLayout.h>
  33. #include <LibGUI/GMenu.h>
  34. #include <LibGUI/GMenuBar.h>
  35. #include <LibGUI/GStatusBar.h>
  36. #include <LibGUI/GTextBox.h>
  37. #include <LibGUI/GToolBar.h>
  38. #include <LibGUI/GWindow.h>
  39. #include <LibHTML/CSS/StyleResolver.h>
  40. #include <LibHTML/DOM/Element.h>
  41. #include <LibHTML/DOMTreeModel.h>
  42. #include <LibHTML/Dump.h>
  43. #include <LibHTML/HtmlView.h>
  44. #include <LibHTML/Layout/LayoutBlock.h>
  45. #include <LibHTML/Layout/LayoutDocument.h>
  46. #include <LibHTML/Layout/LayoutInline.h>
  47. #include <LibHTML/Layout/LayoutNode.h>
  48. #include <LibHTML/Parser/CSSParser.h>
  49. #include <LibHTML/Parser/HTMLParser.h>
  50. #include <LibHTML/ResourceLoader.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. static const char* home_url = "file:///home/anon/www/welcome.html";
  54. int main(int argc, char** argv)
  55. {
  56. if (pledge("stdio shared_buffer accept unix cpath rpath fattr", nullptr) < 0) {
  57. perror("pledge");
  58. return 1;
  59. }
  60. GUI::Application app(argc, argv);
  61. // Connect to the ProtocolServer immediately so we can drop the "unix" pledge.
  62. ResourceLoader::the();
  63. if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
  64. perror("pledge");
  65. return 1;
  66. }
  67. auto window = GUI::Window::construct();
  68. window->set_rect(100, 100, 640, 480);
  69. auto widget = GUI::Widget::construct();
  70. widget->set_fill_with_background_color(true);
  71. widget->set_layout(make<GUI::VBoxLayout>());
  72. widget->layout()->set_spacing(0);
  73. auto toolbar = GUI::ToolBar::construct(widget);
  74. auto html_widget = HtmlView::construct(widget);
  75. History<URL> history;
  76. RefPtr<GUI::Action> go_back_action;
  77. RefPtr<GUI::Action> go_forward_action;
  78. auto update_actions = [&]() {
  79. go_back_action->set_enabled(history.can_go_back());
  80. go_forward_action->set_enabled(history.can_go_forward());
  81. };
  82. bool should_push_loads_to_history = true;
  83. go_back_action = GUI::CommonActions::make_go_back_action([&](auto&) {
  84. history.go_back();
  85. update_actions();
  86. TemporaryChange<bool> change(should_push_loads_to_history, false);
  87. html_widget->load(history.current());
  88. });
  89. go_forward_action = GUI::CommonActions::make_go_forward_action([&](auto&) {
  90. history.go_forward();
  91. update_actions();
  92. TemporaryChange<bool> change(should_push_loads_to_history, false);
  93. html_widget->load(history.current());
  94. });
  95. toolbar->add_action(*go_back_action);
  96. toolbar->add_action(*go_forward_action);
  97. toolbar->add_action(GUI::CommonActions::make_go_home_action([&](auto&) {
  98. html_widget->load(home_url);
  99. }));
  100. toolbar->add_action(GUI::CommonActions::make_reload_action([&](auto&) {
  101. TemporaryChange<bool> change(should_push_loads_to_history, false);
  102. html_widget->reload();
  103. }));
  104. auto location_box = GUI::TextBox::construct(toolbar);
  105. location_box->on_return_pressed = [&] {
  106. html_widget->load(location_box->text());
  107. };
  108. html_widget->on_load_start = [&](auto& url) {
  109. location_box->set_text(url.to_string());
  110. if (should_push_loads_to_history)
  111. history.push(url);
  112. update_actions();
  113. };
  114. html_widget->on_link_click = [&](auto& url) {
  115. if (url.starts_with("#")) {
  116. html_widget->scroll_to_anchor(url.substring_view(1, url.length() - 1));
  117. } else {
  118. html_widget->load(html_widget->document()->complete_url(url));
  119. }
  120. };
  121. html_widget->on_title_change = [&](auto& title) {
  122. window->set_title(String::format("%s - Browser", title.characters()));
  123. };
  124. auto focus_location_box_action = GUI::Action::create("Focus location box", { Mod_Ctrl, Key_L }, [&](auto&) {
  125. location_box->select_all();
  126. location_box->set_focus(true);
  127. });
  128. auto statusbar = GUI::StatusBar::construct(widget);
  129. html_widget->on_link_hover = [&](auto& href) {
  130. statusbar->set_text(href);
  131. };
  132. ResourceLoader::the().on_load_counter_change = [&] {
  133. if (ResourceLoader::the().pending_loads() == 0) {
  134. statusbar->set_text("");
  135. return;
  136. }
  137. statusbar->set_text(String::format("Loading (%d pending resources...)", ResourceLoader::the().pending_loads()));
  138. };
  139. auto menubar = make<GUI::MenuBar>();
  140. auto app_menu = GUI::Menu::construct("Browser");
  141. app_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  142. app.quit();
  143. }));
  144. menubar->add_menu(move(app_menu));
  145. RefPtr<GUI::Window> dom_inspector_window;
  146. auto inspect_menu = GUI::Menu::construct("Inspect");
  147. inspect_menu->add_action(GUI::Action::create("View source", { Mod_Ctrl, Key_U }, [&](auto&) {
  148. String filename_to_open;
  149. char tmp_filename[] = "/tmp/view-source.XXXXXX";
  150. ASSERT(html_widget->document());
  151. if (html_widget->document()->url().protocol() == "file") {
  152. filename_to_open = html_widget->document()->url().path();
  153. } else {
  154. int fd = mkstemp(tmp_filename);
  155. ASSERT(fd >= 0);
  156. auto source = html_widget->document()->source();
  157. write(fd, source.characters(), source.length());
  158. close(fd);
  159. filename_to_open = tmp_filename;
  160. }
  161. if (fork() == 0) {
  162. execl("/bin/TextEditor", "TextEditor", filename_to_open.characters(), nullptr);
  163. ASSERT_NOT_REACHED();
  164. }
  165. }));
  166. inspect_menu->add_action(GUI::Action::create("Inspect DOM tree", { Mod_None, Key_F12 }, [&](auto&) {
  167. if (!dom_inspector_window) {
  168. dom_inspector_window = GUI::Window::construct();
  169. dom_inspector_window->set_rect(100, 100, 300, 500);
  170. dom_inspector_window->set_title("DOM inspector");
  171. auto dom_inspector_widget = InspectorWidget::construct(nullptr);
  172. dom_inspector_window->set_main_widget(dom_inspector_widget);
  173. }
  174. auto* inspector_widget = static_cast<InspectorWidget*>(dom_inspector_window->main_widget());
  175. inspector_widget->set_document(html_widget->document());
  176. dom_inspector_window->show();
  177. dom_inspector_window->move_to_front();
  178. }));
  179. menubar->add_menu(move(inspect_menu));
  180. auto debug_menu = GUI::Menu::construct("Debug");
  181. debug_menu->add_action(GUI::Action::create("Dump DOM tree", [&](auto&) {
  182. dump_tree(*html_widget->document());
  183. }));
  184. debug_menu->add_action(GUI::Action::create("Dump Layout tree", [&](auto&) {
  185. dump_tree(*html_widget->document()->layout_node());
  186. }));
  187. debug_menu->add_action(GUI::Action::create("Dump Style sheets", [&](auto&) {
  188. for (auto& sheet : html_widget->document()->stylesheets()) {
  189. dump_sheet(sheet);
  190. }
  191. }));
  192. debug_menu->add_separator();
  193. auto line_box_borders_action = GUI::Action::create("Line box borders", [&](auto& action) {
  194. action.set_checked(!action.is_checked());
  195. html_widget->set_should_show_line_box_borders(action.is_checked());
  196. html_widget->update();
  197. });
  198. line_box_borders_action->set_checkable(true);
  199. line_box_borders_action->set_checked(false);
  200. debug_menu->add_action(line_box_borders_action);
  201. menubar->add_menu(move(debug_menu));
  202. auto help_menu = GUI::Menu::construct("Help");
  203. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  204. GUI::AboutDialog::show("Browser", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-html.png"), window);
  205. }));
  206. menubar->add_menu(move(help_menu));
  207. app.set_menubar(move(menubar));
  208. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  209. window->set_title("Browser");
  210. window->set_main_widget(widget);
  211. window->show();
  212. URL url_to_load = home_url;
  213. if (app.args().size() >= 1) {
  214. url_to_load = URL();
  215. url_to_load.set_protocol("file");
  216. url_to_load.set_path(app.args()[0]);
  217. }
  218. html_widget->load(url_to_load);
  219. return app.exec();
  220. }