HtmlView.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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* document)
  45. {
  46. if (document == m_document)
  47. return;
  48. if (m_document)
  49. m_document->on_layout_updated = nullptr;
  50. m_document = document;
  51. if (m_document) {
  52. m_document->on_layout_updated = [this] {
  53. layout_and_sync_size();
  54. update();
  55. };
  56. }
  57. main_frame().set_document(document);
  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(), m_document->background_color());
  104. if (auto background_bitmap = m_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 = m_document->hovered_node() && m_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 != m_document->hovered_node();
  126. m_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. }
  137. if (window())
  138. window()->set_override_cursor(is_hovering_link ? GStandardCursor::Hand : GStandardCursor::None);
  139. if (hovered_node_changed) {
  140. update();
  141. auto* hovered_html_element = m_document->hovered_node() ? m_document->hovered_node()->enclosing_html_element() : nullptr;
  142. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  143. auto screen_position = screen_relative_rect().location().translated(event.position());
  144. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  145. } else {
  146. GApplication::the().hide_tooltip();
  147. }
  148. }
  149. if (is_hovering_link != was_hovering_link) {
  150. if (on_link_hover) {
  151. on_link_hover(hovered_link_element ? m_document->complete_url(hovered_link_element->href()).to_string() : String());
  152. }
  153. }
  154. event.accept();
  155. }
  156. void HtmlView::mousedown_event(GMouseEvent& event)
  157. {
  158. if (!layout_root())
  159. return GScrollableWidget::mousemove_event(event);
  160. bool hovered_node_changed = false;
  161. auto result = layout_root()->hit_test(to_content_position(event.position()));
  162. if (result.layout_node) {
  163. auto* node = result.layout_node->node();
  164. hovered_node_changed = node != m_document->hovered_node();
  165. m_document->set_hovered_node(const_cast<Node*>(node));
  166. if (node) {
  167. if (auto* link = node->enclosing_link_element()) {
  168. dbg() << "HtmlView: clicking on a link to " << link->href();
  169. if (on_link_click)
  170. on_link_click(link->href());
  171. }
  172. }
  173. }
  174. if (hovered_node_changed)
  175. update();
  176. event.accept();
  177. }
  178. void HtmlView::keydown_event(GKeyEvent& event)
  179. {
  180. if (event.modifiers() == 0) {
  181. switch (event.key()) {
  182. case Key_Home:
  183. vertical_scrollbar().set_value(0);
  184. break;
  185. case Key_End:
  186. vertical_scrollbar().set_value(vertical_scrollbar().max());
  187. break;
  188. case Key_Down:
  189. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  190. break;
  191. case Key_Up:
  192. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  193. break;
  194. case Key_Left:
  195. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  196. break;
  197. case Key_Right:
  198. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  199. break;
  200. case Key_PageDown:
  201. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  202. break;
  203. case Key_PageUp:
  204. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  205. break;
  206. }
  207. }
  208. event.accept();
  209. }
  210. void HtmlView::reload()
  211. {
  212. load(main_frame().document()->url());
  213. }
  214. static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL& url)
  215. {
  216. auto document = adopt(*new Document);
  217. document->set_url(url);
  218. auto bitmap = load_png_from_memory(data.data(), data.size());
  219. ASSERT(bitmap);
  220. auto html_element = create_element(document, "html");
  221. document->append_child(html_element);
  222. auto head_element = create_element(document, "head");
  223. html_element->append_child(head_element);
  224. auto title_element = create_element(document, "title");
  225. head_element->append_child(title_element);
  226. auto basename = FileSystemPath(url.path()).basename();
  227. auto title_text = adopt(*new Text(document, String::format("%s [%dx%d]", basename.characters(), bitmap->width(), bitmap->height())));
  228. title_element->append_child(title_text);
  229. auto body_element = create_element(document, "body");
  230. html_element->append_child(body_element);
  231. auto image_element = create_element(document, "img");
  232. image_element->set_attribute("src", url.to_string());
  233. body_element->append_child(image_element);
  234. return document;
  235. }
  236. void HtmlView::load(const URL& url)
  237. {
  238. dbg() << "HtmlView::load: " << url;
  239. if (window())
  240. window()->set_override_cursor(GStandardCursor::None);
  241. if (on_load_start)
  242. on_load_start(url);
  243. ResourceLoader::the().load(url, [=](auto data) {
  244. if (data.is_null()) {
  245. dbg() << "Load failed!";
  246. ASSERT_NOT_REACHED();
  247. }
  248. RefPtr<Document> document;
  249. if (url.path().ends_with(".png")) {
  250. document = create_image_document(data, url);
  251. } else {
  252. document = parse_html(data, url);
  253. }
  254. ASSERT(document);
  255. set_document(document);
  256. if (on_title_change)
  257. on_title_change(document->title());
  258. });
  259. }
  260. const LayoutDocument* HtmlView::layout_root() const
  261. {
  262. return document() ? document()->layout_node() : nullptr;
  263. }
  264. LayoutDocument* HtmlView::layout_root()
  265. {
  266. if (!document())
  267. return nullptr;
  268. return const_cast<LayoutDocument*>(document()->layout_node());
  269. }