LayoutNode.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. void LayoutNode::split_into_lines(LayoutBlock& container)
  58. {
  59. for_each_child([&](auto& child) {
  60. if (child.is_inline()) {
  61. child.split_into_lines(container);
  62. } else {
  63. // FIXME: Support block children of inlines.
  64. }
  65. });
  66. }
  67. void LayoutNode::set_needs_display()
  68. {
  69. auto* frame = document().frame();
  70. ASSERT(frame);
  71. for_each_fragment_of_this([&](auto& fragment) {
  72. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  73. const_cast<Frame*>(frame)->set_needs_display(fragment.rect());
  74. }
  75. return IterationDecision::Continue;
  76. });
  77. }