HtmlView.cpp 5.9 KB

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