HtmlView.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <LibGUI/GApplication.h>
  2. #include <LibGUI/GPainter.h>
  3. #include <LibGUI/GScrollBar.h>
  4. #include <LibHTML/DOM/Element.h>
  5. #include <LibHTML/DOM/HTMLAnchorElement.h>
  6. #include <LibHTML/Dump.h>
  7. #include <LibHTML/HtmlView.h>
  8. #include <LibHTML/Layout/LayoutNode.h>
  9. #include <LibHTML/RenderingContext.h>
  10. #include <stdio.h>
  11. HtmlView::HtmlView(GWidget* parent)
  12. : GScrollableWidget(parent)
  13. {
  14. set_frame_shape(FrameShape::Container);
  15. set_frame_shadow(FrameShadow::Sunken);
  16. set_frame_thickness(2);
  17. set_should_hide_unnecessary_scrollbars(true);
  18. set_background_color(Color::White);
  19. }
  20. void HtmlView::set_document(Document* document)
  21. {
  22. if (document == m_document)
  23. return;
  24. m_document = document;
  25. if (document == nullptr)
  26. m_layout_root = nullptr;
  27. else
  28. m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
  29. #ifdef HTML_DEBUG
  30. if (document != nullptr) {
  31. printf("\033[33;1mLayout tree before layout:\033[0m\n");
  32. ::dump_tree(*m_layout_root);
  33. }
  34. #endif
  35. layout_and_sync_size();
  36. update();
  37. }
  38. void HtmlView::layout_and_sync_size()
  39. {
  40. if (!m_layout_root)
  41. return;
  42. m_layout_root->style().size().set_width(available_size().width());
  43. m_layout_root->layout();
  44. set_content_size(m_layout_root->rect().size());
  45. #ifdef HTML_DEBUG
  46. printf("\033[33;1mLayout tree after layout:\033[0m\n");
  47. ::dump_tree(*m_layout_root);
  48. #endif
  49. }
  50. void HtmlView::resize_event(GResizeEvent& event)
  51. {
  52. GScrollableWidget::resize_event(event);
  53. layout_and_sync_size();
  54. }
  55. void HtmlView::paint_event(GPaintEvent& event)
  56. {
  57. GFrame::paint_event(event);
  58. GPainter painter(*this);
  59. painter.add_clip_rect(widget_inner_rect());
  60. painter.add_clip_rect(event.rect());
  61. painter.fill_rect(event.rect(), background_color());
  62. painter.translate(frame_thickness(), frame_thickness());
  63. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  64. if (!m_layout_root)
  65. return;
  66. RenderingContext context { painter };
  67. m_layout_root->render(context);
  68. }
  69. void HtmlView::mousemove_event(GMouseEvent& event)
  70. {
  71. if (!m_layout_root)
  72. return GScrollableWidget::mousemove_event(event);
  73. bool hovered_node_changed = false;
  74. auto result = m_layout_root->hit_test(event.position());
  75. if (result.layout_node) {
  76. auto* node = result.layout_node->node();
  77. hovered_node_changed = node != m_document->hovered_node();
  78. m_document->set_hovered_node(const_cast<Node*>(node));
  79. #ifdef HTML_DEBUG
  80. if (node) {
  81. dbg() << "HtmlView: mousemove: " << node->tag_name() << "{" << node << "}";
  82. if (auto* link = node->enclosing_link_element()) {
  83. dbg() << "HtmlView: hovering over a link to " << link->href();
  84. }
  85. }
  86. #endif
  87. }
  88. if (hovered_node_changed) {
  89. update();
  90. auto* hovered_html_element = m_document->hovered_node() ? m_document->hovered_node()->enclosing_html_element() : nullptr;
  91. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  92. auto screen_position = screen_relative_rect().location().translated(event.position());
  93. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  94. } else {
  95. GApplication::the().hide_tooltip();
  96. }
  97. }
  98. event.accept();
  99. }
  100. void HtmlView::mousedown_event(GMouseEvent& event)
  101. {
  102. if (!m_layout_root)
  103. return GScrollableWidget::mousemove_event(event);
  104. bool hovered_node_changed = false;
  105. auto result = m_layout_root->hit_test(event.position());
  106. if (result.layout_node) {
  107. auto* node = result.layout_node->node();
  108. hovered_node_changed = node != m_document->hovered_node();
  109. m_document->set_hovered_node(const_cast<Node*>(node));
  110. if (node) {
  111. #ifdef HTML_DEBUG
  112. dbg() << "HtmlView: mousedown: " << node->tag_name() << "{" << node << "}";
  113. #endif
  114. if (auto* link = node->enclosing_link_element()) {
  115. dbg() << "HtmlView: clicking on a link to " << link->href();
  116. if (on_link_click)
  117. on_link_click(link->href());
  118. }
  119. }
  120. }
  121. if (hovered_node_changed)
  122. update();
  123. event.accept();
  124. }