LayoutNode.cpp 4.9 KB

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