LayoutNode.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //#define DRAW_BOXES_AROUND_LAYOUT_NODES
  8. //#define DRAW_BOXES_AROUND_HOVERED_NODES
  9. LayoutNode::LayoutNode(const Node* node)
  10. : m_node(node)
  11. {
  12. if (m_node)
  13. m_node->set_layout_node({}, this);
  14. }
  15. LayoutNode::~LayoutNode()
  16. {
  17. if (m_node && m_node->layout_node() == this)
  18. m_node->set_layout_node({}, nullptr);
  19. }
  20. void LayoutNode::layout()
  21. {
  22. for_each_child([](auto& child) {
  23. child.layout();
  24. });
  25. }
  26. const LayoutBlock* LayoutNode::containing_block() const
  27. {
  28. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  29. if (ancestor->is_block())
  30. return static_cast<const LayoutBlock*>(ancestor);
  31. }
  32. return nullptr;
  33. }
  34. void LayoutNode::render(RenderingContext& context)
  35. {
  36. if (!is_visible())
  37. return;
  38. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  39. context.painter().draw_rect(m_rect, Color::Blue);
  40. #endif
  41. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  42. if (!is_anonymous() && node() == document().hovered_node())
  43. context.painter().draw_rect(m_rect, Color::Red);
  44. #endif
  45. Rect padded_rect;
  46. padded_rect.set_x(rect().x() - box_model().padding().left.to_px());
  47. padded_rect.set_width(rect().width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  48. padded_rect.set_y(rect().y() - box_model().padding().top.to_px());
  49. padded_rect.set_height(rect().height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  50. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  51. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  52. context.painter().fill_rect(padded_rect, bgcolor.value()->to_color(document()));
  53. }
  54. // FIXME: Respect all individual border sides
  55. auto border_width_value = style().property(CSS::PropertyID::BorderTopWidth);
  56. auto border_color_value = style().property(CSS::PropertyID::BorderTopColor);
  57. auto border_style_value = style().property(CSS::PropertyID::BorderTopStyle);
  58. if (border_width_value.has_value() && border_color_value.has_value()) {
  59. int border_width = border_width_value.value()->to_length().to_px();
  60. Color border_color = border_color_value.value()->to_color(document());
  61. if (border_style_value.has_value() && border_style_value.value()->to_string() == "inset") {
  62. // border-style: inset
  63. auto shadow_color = Color::from_rgb(0x888888);
  64. auto highlight_color = Color::from_rgb(0x5a5a5a);
  65. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), highlight_color, border_width);
  66. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), shadow_color, border_width);
  67. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), shadow_color, border_width);
  68. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), highlight_color, border_width);
  69. } else if (border_style_value.has_value() && border_style_value.value()->to_string() == "outset") {
  70. // border-style: outset
  71. auto highlight_color = Color::from_rgb(0x888888);
  72. auto shadow_color = Color::from_rgb(0x5a5a5a);
  73. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), highlight_color, border_width);
  74. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), shadow_color, border_width);
  75. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), shadow_color, border_width);
  76. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), highlight_color, border_width);
  77. } else {
  78. // border-style: solid
  79. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), border_color, border_width);
  80. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), border_color, border_width);
  81. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), border_color, border_width);
  82. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), border_color, border_width);
  83. }
  84. }
  85. // TODO: render our border
  86. for_each_child([&](auto& child) {
  87. child.render(context);
  88. });
  89. }
  90. HitTestResult LayoutNode::hit_test(const Point& position) const
  91. {
  92. // FIXME: It would be nice if we could confidently skip over hit testing
  93. // parts of the layout tree, but currently we can't just check
  94. // m_rect.contains() since inline text rects can't be trusted..
  95. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  96. for_each_child([&](auto& child) {
  97. auto child_result = child.hit_test(position);
  98. if (child_result.layout_node)
  99. result = child_result;
  100. });
  101. return result;
  102. }
  103. const Document& LayoutNode::document() const
  104. {
  105. if (is_anonymous())
  106. return parent()->document();
  107. return node()->document();
  108. }
  109. void LayoutNode::split_into_lines(LayoutBlock& container)
  110. {
  111. for_each_child([&](auto& child) {
  112. if (child.is_inline()) {
  113. child.split_into_lines(container);
  114. } else {
  115. // FIXME: Support block children of inlines.
  116. }
  117. });
  118. }
  119. void LayoutNode::set_needs_display()
  120. {
  121. auto* frame = document().frame();
  122. ASSERT(frame);
  123. if (!is_inline()) {
  124. const_cast<Frame*>(frame)->set_needs_display(rect());
  125. return;
  126. }
  127. for_each_fragment_of_this([&](auto& fragment) {
  128. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  129. const_cast<Frame*>(frame)->set_needs_display(fragment.rect());
  130. }
  131. return IterationDecision::Continue;
  132. });
  133. }