LayoutNode.cpp 5.2 KB

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