LayoutNode.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/Layout/LayoutBlock.h>
  5. #include <LibHTML/Layout/LayoutNode.h>
  6. //#define DRAW_BOXES_AROUND_LAYOUT_NODES
  7. //#define DRAW_BOXES_AROUND_HOVERED_NODES
  8. LayoutNode::LayoutNode(const Node* node, RefPtr<StyleProperties> style_properties)
  9. : m_node(node)
  10. , m_style_properties(move(style_properties))
  11. {
  12. if (m_node)
  13. m_node->set_layout_node({}, this);
  14. }
  15. LayoutNode::~LayoutNode()
  16. {
  17. if (m_node)
  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. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  37. context.painter().draw_rect(m_rect, Color::Blue);
  38. #endif
  39. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  40. if (!is_anonymous() && node() == document().hovered_node())
  41. context.painter().draw_rect(m_rect, Color::Red);
  42. #endif
  43. Rect padded_rect;
  44. padded_rect.set_x(rect().x() - box_model().padding().left.to_px());
  45. padded_rect.set_width(rect().width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  46. padded_rect.set_y(rect().y() - box_model().padding().top.to_px());
  47. padded_rect.set_height(rect().height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  48. auto bgcolor = style().property("background-color");
  49. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  50. context.painter().fill_rect(padded_rect, bgcolor.value()->to_color());
  51. }
  52. auto border_width_value = style().property("border-width");
  53. auto border_color_value = style().property("border-color");
  54. auto border_style_value = style().property("border-style");
  55. if (border_width_value.has_value() && border_color_value.has_value()) {
  56. int border_width = border_width_value.value()->to_length().to_px();
  57. Color border_color = border_color_value.value()->to_color();
  58. if (border_style_value.has_value() && border_style_value.value()->to_string() == "inset") {
  59. // border-style: inset
  60. auto shadow_color = Color::from_rgb(0x888888);
  61. auto highlight_color = Color::from_rgb(0x5a5a5a);
  62. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), highlight_color, border_width);
  63. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), shadow_color, border_width);
  64. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), shadow_color, border_width);
  65. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), highlight_color, border_width);
  66. } else if (border_style_value.has_value() && border_style_value.value()->to_string() == "outset") {
  67. // border-style: outset
  68. auto highlight_color = Color::from_rgb(0x888888);
  69. auto shadow_color = Color::from_rgb(0x5a5a5a);
  70. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), highlight_color, border_width);
  71. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), shadow_color, border_width);
  72. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), shadow_color, border_width);
  73. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), highlight_color, border_width);
  74. } else {
  75. // border-style: solid
  76. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), border_color, border_width);
  77. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), border_color, border_width);
  78. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), border_color, border_width);
  79. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), border_color, border_width);
  80. }
  81. }
  82. // TODO: render our border
  83. for_each_child([&](auto& child) {
  84. child.render(context);
  85. });
  86. }
  87. HitTestResult LayoutNode::hit_test(const Point& position) const
  88. {
  89. // FIXME: It would be nice if we could confidently skip over hit testing
  90. // parts of the layout tree, but currently we can't just check
  91. // m_rect.contains() since inline text rects can't be trusted..
  92. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  93. for_each_child([&](auto& child) {
  94. auto child_result = child.hit_test(position);
  95. if (child_result.layout_node)
  96. result = child_result;
  97. });
  98. return result;
  99. }
  100. const Document& LayoutNode::document() const
  101. {
  102. if (is_anonymous())
  103. return parent()->document();
  104. return node()->document();
  105. }
  106. void LayoutNode::split_into_lines(LayoutBlock& container)
  107. {
  108. for_each_child([&](auto& child) {
  109. if (child.is_inline()) {
  110. child.split_into_lines(container);
  111. } else {
  112. // FIXME: Support block children of inlines.
  113. }
  114. });
  115. }