main.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "History.h"
  2. #include <LibCore/CFile.h>
  3. #include <LibGUI/GAboutDialog.h>
  4. #include <LibGUI/GAction.h>
  5. #include <LibGUI/GApplication.h>
  6. #include <LibGUI/GBoxLayout.h>
  7. #include <LibGUI/GMenu.h>
  8. #include <LibGUI/GMenuBar.h>
  9. #include <LibGUI/GStatusBar.h>
  10. #include <LibGUI/GTextBox.h>
  11. #include <LibGUI/GToolBar.h>
  12. #include <LibGUI/GTreeView.h>
  13. #include <LibGUI/GWindow.h>
  14. #include <LibHTML/CSS/StyleResolver.h>
  15. #include <LibHTML/DOM/Element.h>
  16. #include <LibHTML/DOMTreeModel.h>
  17. #include <LibHTML/Dump.h>
  18. #include <LibHTML/HtmlView.h>
  19. #include <LibHTML/Layout/LayoutBlock.h>
  20. #include <LibHTML/Layout/LayoutDocument.h>
  21. #include <LibHTML/Layout/LayoutInline.h>
  22. #include <LibHTML/Layout/LayoutNode.h>
  23. #include <LibHTML/Parser/CSSParser.h>
  24. #include <LibHTML/Parser/HTMLParser.h>
  25. #include <LibHTML/ResourceLoader.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. static const char* home_url = "file:///home/anon/www/welcome.html";
  29. int main(int argc, char** argv)
  30. {
  31. GApplication app(argc, argv);
  32. auto window = GWindow::construct();
  33. window->set_rect(100, 100, 640, 480);
  34. auto widget = GWidget::construct();
  35. widget->set_fill_with_background_color(true);
  36. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  37. widget->layout()->set_spacing(0);
  38. auto toolbar = GToolBar::construct(widget);
  39. auto html_widget = HtmlView::construct(widget);
  40. History<URL> history;
  41. RefPtr<GAction> go_back_action;
  42. RefPtr<GAction> go_forward_action;
  43. auto update_actions = [&]() {
  44. go_back_action->set_enabled(history.can_go_back());
  45. go_forward_action->set_enabled(history.can_go_forward());
  46. };
  47. bool should_push_loads_to_history = true;
  48. go_back_action = GCommonActions::make_go_back_action([&](auto&) {
  49. history.go_back();
  50. update_actions();
  51. TemporaryChange<bool> change(should_push_loads_to_history, false);
  52. html_widget->load(history.current());
  53. });
  54. go_forward_action = GCommonActions::make_go_forward_action([&](auto&) {
  55. history.go_forward();
  56. update_actions();
  57. TemporaryChange<bool> change(should_push_loads_to_history, false);
  58. html_widget->load(history.current());
  59. });
  60. toolbar->add_action(*go_back_action);
  61. toolbar->add_action(*go_forward_action);
  62. toolbar->add_action(GCommonActions::make_go_home_action([&](auto&) {
  63. html_widget->load(home_url);
  64. }));
  65. toolbar->add_action(GCommonActions::make_reload_action([&](auto&) {
  66. TemporaryChange<bool> change(should_push_loads_to_history, false);
  67. html_widget->reload();
  68. }));
  69. auto location_box = GTextBox::construct(toolbar);
  70. location_box->on_return_pressed = [&] {
  71. html_widget->load(location_box->text());
  72. };
  73. html_widget->on_load_start = [&](auto& url) {
  74. location_box->set_text(url.to_string());
  75. if (should_push_loads_to_history)
  76. history.push(url);
  77. update_actions();
  78. };
  79. html_widget->on_link_click = [&](auto& url) {
  80. if (url.starts_with("#")) {
  81. html_widget->scroll_to_anchor(url.substring_view(1, url.length() - 1));
  82. } else {
  83. html_widget->load(html_widget->document()->complete_url(url));
  84. }
  85. };
  86. html_widget->on_title_change = [&](auto& title) {
  87. window->set_title(String::format("%s - Browser", title.characters()));
  88. };
  89. auto focus_location_box_action = GAction::create("Focus location box", { Mod_Ctrl, Key_L }, [&](auto&) {
  90. location_box->select_all();
  91. location_box->set_focus(true);
  92. });
  93. auto statusbar = GStatusBar::construct(widget);
  94. html_widget->on_link_hover = [&](auto& href) {
  95. statusbar->set_text(href);
  96. };
  97. ResourceLoader::the().on_load_counter_change = [&] {
  98. if (ResourceLoader::the().pending_loads() == 0) {
  99. statusbar->set_text("");
  100. return;
  101. }
  102. statusbar->set_text(String::format("Loading (%d pending resources...)", ResourceLoader::the().pending_loads()));
  103. };
  104. auto menubar = make<GMenuBar>();
  105. auto app_menu = GMenu::construct("Browser");
  106. app_menu->add_action(GCommonActions::make_quit_action([&](auto&) {
  107. app.quit();
  108. }));
  109. menubar->add_menu(move(app_menu));
  110. RefPtr<GWindow> dom_inspector_window;
  111. RefPtr<GTreeView> dom_tree_view;
  112. auto inspect_menu = GMenu::construct("Inspect");
  113. inspect_menu->add_action(GAction::create("View source", { Mod_Ctrl, Key_U }, [&](auto&) {
  114. String filename_to_open;
  115. char tmp_filename[] = "/tmp/view-source.XXXXXX";
  116. ASSERT(html_widget->document());
  117. if (html_widget->document()->url().protocol() == "file") {
  118. filename_to_open = html_widget->document()->url().path();
  119. } else {
  120. int fd = mkstemp(tmp_filename);
  121. ASSERT(fd >= 0);
  122. auto source = html_widget->document()->source();
  123. write(fd, source.characters(), source.length());
  124. close(fd);
  125. filename_to_open = tmp_filename;
  126. }
  127. if (fork() == 0) {
  128. execl("/bin/TextEditor", "TextEditor", filename_to_open.characters(), nullptr);
  129. ASSERT_NOT_REACHED();
  130. }
  131. }));
  132. inspect_menu->add_action(GAction::create("Inspect DOM tree", [&](auto&) {
  133. if (!dom_inspector_window) {
  134. dom_inspector_window = GWindow::construct();
  135. dom_inspector_window->set_rect(100, 100, 300, 500);
  136. dom_inspector_window->set_title("DOM inspector");
  137. dom_tree_view = GTreeView::construct(nullptr);
  138. dom_tree_view->on_selection = [](auto& index) {
  139. auto* node = static_cast<Node*>(index.internal_data());
  140. node->document().set_inspected_node(node);
  141. };
  142. dom_inspector_window->set_main_widget(dom_tree_view);
  143. }
  144. if (html_widget->document())
  145. dom_tree_view->set_model(DOMTreeModel::create(*html_widget->document()));
  146. else
  147. dom_tree_view->set_model(nullptr);
  148. dom_inspector_window->show();
  149. dom_inspector_window->move_to_front();
  150. }));
  151. menubar->add_menu(move(inspect_menu));
  152. auto debug_menu = GMenu::construct("Debug");
  153. debug_menu->add_action(GAction::create("Dump DOM tree", [&](auto&) {
  154. dump_tree(*html_widget->document());
  155. }));
  156. debug_menu->add_action(GAction::create("Dump Layout tree", [&](auto&) {
  157. dump_tree(*html_widget->document()->layout_node());
  158. }));
  159. debug_menu->add_action(GAction::create("Dump Style sheets", [&](auto&) {
  160. for (auto& sheet : html_widget->document()->stylesheets()) {
  161. dump_sheet(sheet);
  162. }
  163. }));
  164. debug_menu->add_separator();
  165. auto line_box_borders_action = GAction::create("Line box borders", [&](auto& action) {
  166. action.set_checked(!action.is_checked());
  167. html_widget->set_should_show_line_box_borders(action.is_checked());
  168. html_widget->update();
  169. });
  170. line_box_borders_action->set_checkable(true);
  171. line_box_borders_action->set_checked(false);
  172. debug_menu->add_action(line_box_borders_action);
  173. menubar->add_menu(move(debug_menu));
  174. auto help_menu = GMenu::construct("Help");
  175. help_menu->add_action(GAction::create("About", [&](const GAction&) {
  176. GAboutDialog::show("Browser", GraphicsBitmap::load_from_file("/res/icons/32x32/filetype-html.png"), window);
  177. }));
  178. menubar->add_menu(move(help_menu));
  179. app.set_menubar(move(menubar));
  180. window->set_icon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  181. window->set_title("Browser");
  182. window->set_main_widget(widget);
  183. window->show();
  184. URL url_to_load = home_url;
  185. if (app.args().size() >= 1) {
  186. url_to_load = URL();
  187. url_to_load.set_protocol("file");
  188. url_to_load.set_path(app.args()[0]);
  189. }
  190. html_widget->load(url_to_load);
  191. return app.exec();
  192. }