HtmlView.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <LibCore/CFile.h>
  2. #include <LibGUI/GApplication.h>
  3. #include <LibGUI/GPainter.h>
  4. #include <LibGUI/GScrollBar.h>
  5. #include <LibHTML/DOM/Element.h>
  6. #include <LibHTML/DOM/HTMLAnchorElement.h>
  7. #include <LibHTML/Dump.h>
  8. #include <LibHTML/Frame.h>
  9. #include <LibHTML/HtmlView.h>
  10. #include <LibHTML/Layout/LayoutNode.h>
  11. #include <LibHTML/Parser/HTMLParser.h>
  12. #include <LibHTML/RenderingContext.h>
  13. #include <LibHTML/ResourceLoader.h>
  14. #include <stdio.h>
  15. HtmlView::HtmlView(GWidget* parent)
  16. : GScrollableWidget(parent)
  17. , m_main_frame(Frame::create())
  18. {
  19. set_frame_shape(FrameShape::Container);
  20. set_frame_shadow(FrameShadow::Sunken);
  21. set_frame_thickness(2);
  22. set_should_hide_unnecessary_scrollbars(true);
  23. set_background_color(Color::White);
  24. }
  25. HtmlView::~HtmlView()
  26. {
  27. }
  28. void HtmlView::set_document(Document* document)
  29. {
  30. if (document == m_document)
  31. return;
  32. if (m_document)
  33. m_document->on_invalidate_layout = nullptr;
  34. m_document = document;
  35. if (m_document) {
  36. m_document->on_invalidate_layout = [this]() {
  37. m_layout_root = m_document->create_layout_tree(m_document->style_resolver(), nullptr);
  38. layout_and_sync_size();
  39. update();
  40. };
  41. }
  42. main_frame().set_document(document);
  43. if (document == nullptr)
  44. m_layout_root = nullptr;
  45. else
  46. m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
  47. #ifdef HTML_DEBUG
  48. if (document != nullptr) {
  49. dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n");
  50. ::dump_tree(*m_layout_root);
  51. }
  52. #endif
  53. layout_and_sync_size();
  54. update();
  55. }
  56. void HtmlView::layout_and_sync_size()
  57. {
  58. if (!m_layout_root)
  59. return;
  60. main_frame().set_size(available_size());
  61. m_layout_root->layout();
  62. set_content_size(m_layout_root->rect().size());
  63. #ifdef HTML_DEBUG
  64. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  65. ::dump_tree(*m_layout_root);
  66. #endif
  67. }
  68. void HtmlView::resize_event(GResizeEvent& event)
  69. {
  70. GScrollableWidget::resize_event(event);
  71. layout_and_sync_size();
  72. }
  73. void HtmlView::paint_event(GPaintEvent& event)
  74. {
  75. GFrame::paint_event(event);
  76. GPainter painter(*this);
  77. painter.add_clip_rect(widget_inner_rect());
  78. painter.add_clip_rect(event.rect());
  79. if (!m_layout_root) {
  80. painter.fill_rect(event.rect(), background_color());
  81. return;
  82. }
  83. painter.fill_rect(event.rect(), m_document->background_color());
  84. painter.translate(frame_thickness(), frame_thickness());
  85. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  86. RenderingContext context { painter };
  87. m_layout_root->render(context);
  88. }
  89. void HtmlView::mousemove_event(GMouseEvent& event)
  90. {
  91. if (!m_layout_root)
  92. return GScrollableWidget::mousemove_event(event);
  93. bool hovered_node_changed = false;
  94. auto result = m_layout_root->hit_test(to_content_position(event.position()));
  95. if (result.layout_node) {
  96. auto* node = result.layout_node->node();
  97. hovered_node_changed = node != m_document->hovered_node();
  98. m_document->set_hovered_node(const_cast<Node*>(node));
  99. #ifdef HTML_DEBUG
  100. if (node) {
  101. if (auto* link = node->enclosing_link_element()) {
  102. dbg() << "HtmlView: hovering over a link to " << link->href();
  103. }
  104. }
  105. #endif
  106. }
  107. if (hovered_node_changed) {
  108. update();
  109. auto* hovered_html_element = m_document->hovered_node() ? m_document->hovered_node()->enclosing_html_element() : nullptr;
  110. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  111. auto screen_position = screen_relative_rect().location().translated(event.position());
  112. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  113. } else {
  114. GApplication::the().hide_tooltip();
  115. }
  116. }
  117. event.accept();
  118. }
  119. void HtmlView::mousedown_event(GMouseEvent& event)
  120. {
  121. if (!m_layout_root)
  122. return GScrollableWidget::mousemove_event(event);
  123. bool hovered_node_changed = false;
  124. auto result = m_layout_root->hit_test(to_content_position(event.position()));
  125. if (result.layout_node) {
  126. auto* node = result.layout_node->node();
  127. hovered_node_changed = node != m_document->hovered_node();
  128. m_document->set_hovered_node(const_cast<Node*>(node));
  129. if (node) {
  130. if (auto* link = node->enclosing_link_element()) {
  131. dbg() << "HtmlView: clicking on a link to " << link->href();
  132. if (on_link_click)
  133. on_link_click(link->href());
  134. }
  135. }
  136. }
  137. if (hovered_node_changed)
  138. update();
  139. event.accept();
  140. }
  141. void HtmlView::reload()
  142. {
  143. load(main_frame().document()->url());
  144. }
  145. void HtmlView::load(const URL& url)
  146. {
  147. dbg() << "HtmlView::load: " << url;
  148. if (on_load_start)
  149. on_load_start(url);
  150. ResourceLoader::the().load(url, [=](auto data) {
  151. if (data.is_null()) {
  152. dbg() << "Load failed!";
  153. ASSERT_NOT_REACHED();
  154. }
  155. auto document = parse_html(data, url);
  156. document->normalize();
  157. set_document(document);
  158. if (on_title_change)
  159. on_title_change(document->title());
  160. });
  161. }