HtmlView.cpp 5.7 KB

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