LayoutNode.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/Frame.h>
  5. #include <LibHTML/Layout/LayoutBlock.h>
  6. #include <LibHTML/Layout/LayoutNode.h>
  7. LayoutNode::LayoutNode(const Node* node)
  8. : m_node(node)
  9. {
  10. if (m_node)
  11. m_node->set_layout_node({}, this);
  12. }
  13. LayoutNode::~LayoutNode()
  14. {
  15. if (m_node && m_node->layout_node() == this)
  16. m_node->set_layout_node({}, nullptr);
  17. }
  18. void LayoutNode::layout()
  19. {
  20. for_each_child([](auto& child) {
  21. child.layout();
  22. });
  23. }
  24. const LayoutBlock* LayoutNode::containing_block() const
  25. {
  26. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  27. if (is<LayoutBlock>(*ancestor))
  28. return to<LayoutBlock>(ancestor);
  29. }
  30. return nullptr;
  31. }
  32. void LayoutNode::render(RenderingContext& context)
  33. {
  34. if (!is_visible())
  35. return;
  36. // TODO: render our border
  37. for_each_child([&](auto& child) {
  38. child.render(context);
  39. });
  40. }
  41. HitTestResult LayoutNode::hit_test(const Point& position) const
  42. {
  43. HitTestResult result;
  44. for_each_child([&](auto& child) {
  45. auto child_result = child.hit_test(position);
  46. if (child_result.layout_node)
  47. result = child_result;
  48. });
  49. return result;
  50. }
  51. const Document& LayoutNode::document() const
  52. {
  53. if (is_anonymous())
  54. return parent()->document();
  55. return node()->document();
  56. }
  57. Document& LayoutNode::document()
  58. {
  59. if (is_anonymous())
  60. return parent()->document();
  61. // FIXME: Remove this const_cast once we give up on the idea of a const link from layout tree to DOM tree.
  62. return const_cast<Node*>(node())->document();
  63. }
  64. const LayoutDocument& LayoutNode::root() const
  65. {
  66. ASSERT(document().layout_node());
  67. return *document().layout_node();
  68. }
  69. LayoutDocument& LayoutNode::root()
  70. {
  71. ASSERT(document().layout_node());
  72. return *document().layout_node();
  73. }
  74. void LayoutNode::split_into_lines(LayoutBlock& container)
  75. {
  76. for_each_child([&](auto& child) {
  77. if (child.is_inline()) {
  78. child.split_into_lines(container);
  79. } else {
  80. // FIXME: Support block children of inlines.
  81. }
  82. });
  83. }
  84. void LayoutNode::set_needs_display()
  85. {
  86. auto* frame = document().frame();
  87. ASSERT(frame);
  88. if (auto* block = containing_block()) {
  89. block->for_each_fragment([&](auto& fragment) {
  90. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  91. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(fragment.rect()));
  92. }
  93. return IterationDecision::Continue;
  94. });
  95. }
  96. }
  97. FloatPoint LayoutNode::box_type_agnostic_position() const
  98. {
  99. if (is_box())
  100. return to<LayoutBox>(*this).position();
  101. ASSERT(is_inline());
  102. FloatPoint position;
  103. if (auto* block = containing_block()) {
  104. block->for_each_fragment([&](auto& fragment) {
  105. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  106. position = fragment.rect().location();
  107. return IterationDecision::Break;
  108. }
  109. return IterationDecision::Continue;
  110. });
  111. }
  112. return position;
  113. }