HtmlView.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_layout_updated = nullptr;
  41. m_document = document;
  42. if (m_document) {
  43. m_document->on_layout_updated = [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. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  63. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  64. main_frame().set_size(available_size());
  65. document()->layout();
  66. set_content_size(layout_root()->size());
  67. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  68. // since the scrollbars now take up some of the available space.
  69. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  70. main_frame().set_size(available_size());
  71. document()->layout();
  72. set_content_size(layout_root()->size());
  73. }
  74. #ifdef HTML_DEBUG
  75. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  76. ::dump_tree(*layout_root());
  77. #endif
  78. }
  79. void HtmlView::resize_event(GResizeEvent& event)
  80. {
  81. GScrollableWidget::resize_event(event);
  82. layout_and_sync_size();
  83. }
  84. void HtmlView::paint_event(GPaintEvent& event)
  85. {
  86. GFrame::paint_event(event);
  87. GPainter painter(*this);
  88. painter.add_clip_rect(widget_inner_rect());
  89. painter.add_clip_rect(event.rect());
  90. if (!layout_root()) {
  91. painter.fill_rect(event.rect(), background_color());
  92. return;
  93. }
  94. painter.fill_rect(event.rect(), m_document->background_color());
  95. painter.translate(frame_thickness(), frame_thickness());
  96. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  97. RenderingContext context { painter };
  98. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  99. context.set_viewport_rect(visible_content_rect());
  100. layout_root()->render(context);
  101. }
  102. void HtmlView::mousemove_event(GMouseEvent& event)
  103. {
  104. if (!layout_root())
  105. return GScrollableWidget::mousemove_event(event);
  106. bool hovered_node_changed = false;
  107. bool is_hovering_link = false;
  108. auto result = layout_root()->hit_test(to_content_position(event.position()));
  109. if (result.layout_node) {
  110. auto* node = result.layout_node->node();
  111. hovered_node_changed = node != m_document->hovered_node();
  112. m_document->set_hovered_node(const_cast<Node*>(node));
  113. if (node) {
  114. if (auto* link = node->enclosing_link_element()) {
  115. UNUSED_PARAM(link);
  116. #ifdef HTML_DEBUG
  117. dbg() << "HtmlView: hovering over a link to " << link->href();
  118. #endif
  119. is_hovering_link = true;
  120. }
  121. }
  122. }
  123. if (window())
  124. window()->set_override_cursor(is_hovering_link ? GStandardCursor::Hand : GStandardCursor::None);
  125. if (hovered_node_changed) {
  126. update();
  127. auto* hovered_html_element = m_document->hovered_node() ? m_document->hovered_node()->enclosing_html_element() : nullptr;
  128. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  129. auto screen_position = screen_relative_rect().location().translated(event.position());
  130. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  131. } else {
  132. GApplication::the().hide_tooltip();
  133. }
  134. }
  135. event.accept();
  136. }
  137. void HtmlView::mousedown_event(GMouseEvent& event)
  138. {
  139. if (!layout_root())
  140. return GScrollableWidget::mousemove_event(event);
  141. bool hovered_node_changed = false;
  142. auto result = layout_root()->hit_test(to_content_position(event.position()));
  143. if (result.layout_node) {
  144. auto* node = result.layout_node->node();
  145. hovered_node_changed = node != m_document->hovered_node();
  146. m_document->set_hovered_node(const_cast<Node*>(node));
  147. if (node) {
  148. if (auto* link = node->enclosing_link_element()) {
  149. dbg() << "HtmlView: clicking on a link to " << link->href();
  150. if (on_link_click)
  151. on_link_click(link->href());
  152. }
  153. }
  154. }
  155. if (hovered_node_changed)
  156. update();
  157. event.accept();
  158. }
  159. void HtmlView::reload()
  160. {
  161. load(main_frame().document()->url());
  162. }
  163. void HtmlView::load(const URL& url)
  164. {
  165. dbg() << "HtmlView::load: " << url;
  166. if (window())
  167. window()->set_override_cursor(GStandardCursor::None);
  168. if (on_load_start)
  169. on_load_start(url);
  170. ResourceLoader::the().load(url, [=](auto data) {
  171. if (data.is_null()) {
  172. dbg() << "Load failed!";
  173. ASSERT_NOT_REACHED();
  174. }
  175. auto document = parse_html(data, url);
  176. set_document(document);
  177. if (on_title_change)
  178. on_title_change(document->title());
  179. });
  180. }
  181. const LayoutDocument* HtmlView::layout_root() const
  182. {
  183. return document() ? document()->layout_node() : nullptr;
  184. }
  185. LayoutDocument* HtmlView::layout_root()
  186. {
  187. if (!document())
  188. return nullptr;
  189. return const_cast<LayoutDocument*>(document()->layout_node());
  190. }