HtmlView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include <AK/FileSystemPath.h>
  2. #include <LibCore/CFile.h>
  3. #include <LibDraw/PNGLoader.h>
  4. #include <LibGUI/GApplication.h>
  5. #include <LibGUI/GPainter.h>
  6. #include <LibGUI/GScrollBar.h>
  7. #include <LibGUI/GWindow.h>
  8. #include <LibHTML/DOM/Element.h>
  9. #include <LibHTML/DOM/ElementFactory.h>
  10. #include <LibHTML/DOM/HTMLAnchorElement.h>
  11. #include <LibHTML/DOM/HTMLImageElement.h>
  12. #include <LibHTML/DOM/Text.h>
  13. #include <LibHTML/Dump.h>
  14. #include <LibHTML/Frame.h>
  15. #include <LibHTML/HtmlView.h>
  16. #include <LibHTML/Layout/LayoutDocument.h>
  17. #include <LibHTML/Layout/LayoutNode.h>
  18. #include <LibHTML/Parser/HTMLParser.h>
  19. #include <LibHTML/RenderingContext.h>
  20. #include <LibHTML/ResourceLoader.h>
  21. #include <stdio.h>
  22. HtmlView::HtmlView(GWidget* parent)
  23. : GScrollableWidget(parent)
  24. , m_main_frame(Frame::create())
  25. {
  26. main_frame().on_set_needs_display = [this](auto& content_rect) {
  27. if (content_rect.is_empty()) {
  28. update();
  29. return;
  30. }
  31. Rect adjusted_rect = content_rect;
  32. adjusted_rect.set_location(to_widget_position(content_rect.location()));
  33. update(adjusted_rect);
  34. };
  35. set_frame_shape(FrameShape::Container);
  36. set_frame_shadow(FrameShadow::Sunken);
  37. set_frame_thickness(2);
  38. set_should_hide_unnecessary_scrollbars(true);
  39. set_background_color(Color::White);
  40. }
  41. HtmlView::~HtmlView()
  42. {
  43. }
  44. void HtmlView::set_document(Document* new_document)
  45. {
  46. RefPtr<Document> old_document = document();
  47. if (new_document == old_document)
  48. return;
  49. if (old_document)
  50. old_document->on_layout_updated = nullptr;
  51. main_frame().set_document(new_document);
  52. if (new_document) {
  53. new_document->on_layout_updated = [this] {
  54. layout_and_sync_size();
  55. update();
  56. };
  57. }
  58. #ifdef HTML_DEBUG
  59. if (document != nullptr) {
  60. dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n");
  61. ::dump_tree(*layout_root());
  62. }
  63. #endif
  64. layout_and_sync_size();
  65. update();
  66. }
  67. void HtmlView::layout_and_sync_size()
  68. {
  69. if (!document())
  70. return;
  71. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  72. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  73. main_frame().set_size(available_size());
  74. document()->layout();
  75. set_content_size(layout_root()->size());
  76. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  77. // since the scrollbars now take up some of the available space.
  78. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  79. main_frame().set_size(available_size());
  80. document()->layout();
  81. set_content_size(layout_root()->size());
  82. }
  83. #ifdef HTML_DEBUG
  84. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  85. ::dump_tree(*layout_root());
  86. #endif
  87. }
  88. void HtmlView::resize_event(GResizeEvent& event)
  89. {
  90. GScrollableWidget::resize_event(event);
  91. layout_and_sync_size();
  92. }
  93. void HtmlView::paint_event(GPaintEvent& event)
  94. {
  95. GFrame::paint_event(event);
  96. GPainter painter(*this);
  97. painter.add_clip_rect(widget_inner_rect());
  98. painter.add_clip_rect(event.rect());
  99. if (!layout_root()) {
  100. painter.fill_rect(event.rect(), background_color());
  101. return;
  102. }
  103. painter.fill_rect(event.rect(), document()->background_color());
  104. if (auto background_bitmap = document()->background_image()) {
  105. painter.draw_tiled_bitmap(event.rect(), *background_bitmap);
  106. }
  107. painter.translate(frame_thickness(), frame_thickness());
  108. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  109. RenderingContext context { painter };
  110. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  111. context.set_viewport_rect(visible_content_rect());
  112. layout_root()->render(context);
  113. }
  114. void HtmlView::mousemove_event(GMouseEvent& event)
  115. {
  116. if (!layout_root())
  117. return GScrollableWidget::mousemove_event(event);
  118. bool hovered_node_changed = false;
  119. bool is_hovering_link = false;
  120. bool was_hovering_link = document()->hovered_node() && document()->hovered_node()->is_link();
  121. auto result = layout_root()->hit_test(to_content_position(event.position()));
  122. const HTMLAnchorElement* hovered_link_element = nullptr;
  123. if (result.layout_node) {
  124. auto* node = result.layout_node->node();
  125. hovered_node_changed = node != document()->hovered_node();
  126. document()->set_hovered_node(const_cast<Node*>(node));
  127. if (node) {
  128. hovered_link_element = node->enclosing_link_element();
  129. if (hovered_link_element) {
  130. #ifdef HTML_DEBUG
  131. dbg() << "HtmlView: hovering over a link to " << hovered_link_element->href();
  132. #endif
  133. is_hovering_link = true;
  134. }
  135. }
  136. if (m_in_mouse_selection) {
  137. layout_root()->selection().set_end({ result.layout_node, result.index_in_node });
  138. dump_selection("MouseMove");
  139. update();
  140. }
  141. }
  142. if (window())
  143. window()->set_override_cursor(is_hovering_link ? GStandardCursor::Hand : GStandardCursor::None);
  144. if (hovered_node_changed) {
  145. update();
  146. auto* hovered_html_element = document()->hovered_node() ? document()->hovered_node()->enclosing_html_element() : nullptr;
  147. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  148. auto screen_position = screen_relative_rect().location().translated(event.position());
  149. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  150. } else {
  151. GApplication::the().hide_tooltip();
  152. }
  153. }
  154. if (is_hovering_link != was_hovering_link) {
  155. if (on_link_hover) {
  156. on_link_hover(hovered_link_element ? document()->complete_url(hovered_link_element->href()).to_string() : String());
  157. }
  158. }
  159. event.accept();
  160. }
  161. void HtmlView::mousedown_event(GMouseEvent& event)
  162. {
  163. if (!layout_root())
  164. return GScrollableWidget::mousemove_event(event);
  165. bool hovered_node_changed = false;
  166. auto result = layout_root()->hit_test(to_content_position(event.position()));
  167. if (result.layout_node) {
  168. auto* node = result.layout_node->node();
  169. hovered_node_changed = node != document()->hovered_node();
  170. document()->set_hovered_node(const_cast<Node*>(node));
  171. if (node) {
  172. if (auto* link = node->enclosing_link_element()) {
  173. dbg() << "HtmlView: clicking on a link to " << link->href();
  174. if (on_link_click)
  175. on_link_click(link->href());
  176. } else {
  177. if (event.button() == GMouseButton::Left) {
  178. layout_root()->selection().set({ result.layout_node, result.index_in_node }, {});
  179. dump_selection("MouseDown");
  180. m_in_mouse_selection = true;
  181. }
  182. }
  183. }
  184. }
  185. if (hovered_node_changed)
  186. update();
  187. event.accept();
  188. }
  189. void HtmlView::mouseup_event(GMouseEvent& event)
  190. {
  191. if (!layout_root())
  192. return GScrollableWidget::mouseup_event(event);
  193. if (event.button() == GMouseButton::Left) {
  194. dump_selection("MouseUp");
  195. m_in_mouse_selection = false;
  196. }
  197. }
  198. void HtmlView::keydown_event(GKeyEvent& event)
  199. {
  200. if (event.modifiers() == 0) {
  201. switch (event.key()) {
  202. case Key_Home:
  203. vertical_scrollbar().set_value(0);
  204. break;
  205. case Key_End:
  206. vertical_scrollbar().set_value(vertical_scrollbar().max());
  207. break;
  208. case Key_Down:
  209. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  210. break;
  211. case Key_Up:
  212. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  213. break;
  214. case Key_Left:
  215. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  216. break;
  217. case Key_Right:
  218. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  219. break;
  220. case Key_PageDown:
  221. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  222. break;
  223. case Key_PageUp:
  224. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  225. break;
  226. }
  227. }
  228. event.accept();
  229. }
  230. void HtmlView::reload()
  231. {
  232. load(main_frame().document()->url());
  233. }
  234. static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL& url)
  235. {
  236. auto document = adopt(*new Document);
  237. document->set_url(url);
  238. auto bitmap = load_png_from_memory(data.data(), data.size());
  239. ASSERT(bitmap);
  240. auto html_element = create_element(document, "html");
  241. document->append_child(html_element);
  242. auto head_element = create_element(document, "head");
  243. html_element->append_child(head_element);
  244. auto title_element = create_element(document, "title");
  245. head_element->append_child(title_element);
  246. auto basename = FileSystemPath(url.path()).basename();
  247. auto title_text = adopt(*new Text(document, String::format("%s [%dx%d]", basename.characters(), bitmap->width(), bitmap->height())));
  248. title_element->append_child(title_text);
  249. auto body_element = create_element(document, "body");
  250. html_element->append_child(body_element);
  251. auto image_element = create_element(document, "img");
  252. image_element->set_attribute("src", url.to_string());
  253. body_element->append_child(image_element);
  254. return document;
  255. }
  256. void HtmlView::load(const URL& url)
  257. {
  258. dbg() << "HtmlView::load: " << url;
  259. if (window())
  260. window()->set_override_cursor(GStandardCursor::None);
  261. if (on_load_start)
  262. on_load_start(url);
  263. ResourceLoader::the().load(url, [=](auto data) {
  264. if (data.is_null()) {
  265. dbg() << "Load failed!";
  266. ASSERT_NOT_REACHED();
  267. }
  268. RefPtr<Document> document;
  269. if (url.path().ends_with(".png")) {
  270. document = create_image_document(data, url);
  271. } else {
  272. document = parse_html(data, url);
  273. }
  274. ASSERT(document);
  275. set_document(document);
  276. if (on_title_change)
  277. on_title_change(document->title());
  278. });
  279. }
  280. const LayoutDocument* HtmlView::layout_root() const
  281. {
  282. return document() ? document()->layout_node() : nullptr;
  283. }
  284. LayoutDocument* HtmlView::layout_root()
  285. {
  286. if (!document())
  287. return nullptr;
  288. return const_cast<LayoutDocument*>(document()->layout_node());
  289. }
  290. void HtmlView::scroll_to_anchor(const StringView& name)
  291. {
  292. if (!document())
  293. return;
  294. auto* element = document()->get_element_by_id(name);
  295. if (!element) {
  296. auto candidates = document()->get_elements_by_name(name);
  297. for (auto* candidate : candidates) {
  298. if (is<HTMLAnchorElement>(*candidate)) {
  299. element = to<HTMLAnchorElement>(candidate);
  300. break;
  301. }
  302. }
  303. }
  304. if (!element) {
  305. dbg() << "HtmlView::scroll_to_anchor(): Anchor not found: '" << name << "'";
  306. return;
  307. }
  308. if (!element->layout_node()) {
  309. dbg() << "HtmlView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'";
  310. return;
  311. }
  312. auto& layout_node = *element->layout_node();
  313. scroll_into_view({ layout_node.box_type_agnostic_position(), visible_content_rect().size() }, true, true);
  314. window()->set_override_cursor(GStandardCursor::None);
  315. }
  316. Document* HtmlView::document()
  317. {
  318. return main_frame().document();
  319. }
  320. const Document* HtmlView::document() const
  321. {
  322. return main_frame().document();
  323. }
  324. void HtmlView::dump_selection(const char* event_name)
  325. {
  326. dbg() << event_name << " selection start: "
  327. << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: "
  328. << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node;
  329. }