LayoutBox.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/HTMLBodyElement.h>
  4. #include <LibHTML/Frame.h>
  5. #include <LibHTML/Layout/LayoutBlock.h>
  6. #include <LibHTML/Layout/LayoutBox.h>
  7. //#define DRAW_BOXES_AROUND_LAYOUT_NODES
  8. //#define DRAW_BOXES_AROUND_HOVERED_NODES
  9. void LayoutBox::paint_border(RenderingContext& context, Edge edge, const Rect& rect, CSS::PropertyID style_property_id, CSS::PropertyID color_property_id, CSS::PropertyID width_property_id)
  10. {
  11. auto border_width = style().property(width_property_id);
  12. if (!border_width.has_value())
  13. return;
  14. auto border_style = style().property(style_property_id);
  15. float width = border_width.value()->to_length().to_px();
  16. Color color;
  17. auto border_color = style().property(color_property_id);
  18. if (border_color.has_value()) {
  19. color = border_color.value()->to_color(document());
  20. } else {
  21. // FIXME: This is basically CSS "currentColor" which should be handled elsewhere
  22. // in a much more reusable way.
  23. auto current_color = style().property(CSS::PropertyID::Color);
  24. if (current_color.has_value())
  25. color = current_color.value()->to_color(document());
  26. else
  27. color = Color::Black;
  28. }
  29. auto first_point_for_edge = [](Edge edge, const Rect& rect) {
  30. switch (edge) {
  31. case Edge::Top:
  32. return rect.top_left();
  33. case Edge::Right:
  34. return rect.top_right();
  35. case Edge::Bottom:
  36. return rect.bottom_left();
  37. case Edge::Left:
  38. default:
  39. return rect.top_left();
  40. }
  41. };
  42. auto second_point_for_edge = [](Edge edge, const Rect& rect) {
  43. switch (edge) {
  44. case Edge::Top:
  45. return rect.top_right();
  46. case Edge::Right:
  47. return rect.bottom_right();
  48. case Edge::Bottom:
  49. return rect.bottom_right();
  50. case Edge::Left:
  51. default:
  52. return rect.bottom_left();
  53. }
  54. };
  55. auto p1 = first_point_for_edge(edge, rect);
  56. auto p2 = second_point_for_edge(edge, rect);
  57. if (border_style.has_value() && border_style.value()->to_string() == "inset") {
  58. auto top_left_color = Color::from_rgb(0x5a5a5a);
  59. auto bottom_right_color = Color::from_rgb(0x888888);
  60. color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
  61. } else if (border_style.has_value() && border_style.value()->to_string() == "outset") {
  62. auto top_left_color = Color::from_rgb(0x888888);
  63. auto bottom_right_color = Color::from_rgb(0x5a5a5a);
  64. color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
  65. } else {
  66. // border-style: solid
  67. }
  68. dbg() << "draw_line(" << p1 << ", " << p2 << ", " << color.to_string() << ", " << (int)width << ")";
  69. context.painter().draw_line(p1, p2, color, width);
  70. }
  71. void LayoutBox::render(RenderingContext& context)
  72. {
  73. if (!is_visible())
  74. return;
  75. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  76. context.painter().draw_rect(m_rect, Color::Blue);
  77. #endif
  78. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  79. if (!is_anonymous() && node() == document().hovered_node())
  80. context.painter().draw_rect(m_rect, Color::Red);
  81. #endif
  82. if (node() && document().inspected_node() == node())
  83. context.painter().draw_rect(enclosing_int_rect(m_rect), Color::Magenta);
  84. Rect padded_rect;
  85. padded_rect.set_x(x() - box_model().padding().left.to_px());
  86. padded_rect.set_width(width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  87. padded_rect.set_y(y() - box_model().padding().top.to_px());
  88. padded_rect.set_height(height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  89. if (!is_body()) {
  90. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  91. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  92. context.painter().fill_rect(padded_rect, bgcolor.value()->to_color(document()));
  93. }
  94. auto bgimage = style().property(CSS::PropertyID::BackgroundImage);
  95. if (bgimage.has_value() && bgimage.value()->is_image()) {
  96. auto& image_value = static_cast<const ImageStyleValue&>(*bgimage.value());
  97. if (image_value.bitmap()) {
  98. context.painter().draw_tiled_bitmap(padded_rect, *image_value.bitmap());
  99. }
  100. }
  101. }
  102. Rect bordered_rect;
  103. bordered_rect.set_x(padded_rect.x() - box_model().border().left.to_px());
  104. bordered_rect.set_width(padded_rect.width() + box_model().border().left.to_px() + box_model().border().right.to_px());
  105. bordered_rect.set_y(padded_rect.y() - box_model().border().top.to_px());
  106. bordered_rect.set_height(padded_rect.height() + box_model().border().top.to_px() + box_model().border().bottom.to_px());
  107. paint_border(context, Edge::Top, bordered_rect, CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopWidth);
  108. paint_border(context, Edge::Right, bordered_rect, CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightWidth);
  109. paint_border(context, Edge::Bottom, bordered_rect, CSS::PropertyID::BorderBottomStyle, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomWidth);
  110. paint_border(context, Edge::Left, bordered_rect, CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftWidth);
  111. LayoutNodeWithStyleAndBoxModelMetrics::render(context);
  112. }
  113. HitTestResult LayoutBox::hit_test(const Point& position) const
  114. {
  115. // FIXME: It would be nice if we could confidently skip over hit testing
  116. // parts of the layout tree, but currently we can't just check
  117. // m_rect.contains() since inline text rects can't be trusted..
  118. HitTestResult result { m_rect.contains(FloatPoint(position.x(), position.y())) ? this : nullptr };
  119. for_each_child([&](auto& child) {
  120. auto child_result = child.hit_test(position);
  121. if (child_result.layout_node)
  122. result = child_result;
  123. });
  124. return result;
  125. }
  126. void LayoutBox::set_needs_display()
  127. {
  128. auto* frame = document().frame();
  129. ASSERT(frame);
  130. if (!is_inline()) {
  131. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(rect()));
  132. return;
  133. }
  134. LayoutNode::set_needs_display();
  135. }
  136. bool LayoutBox::is_body() const
  137. {
  138. return node() && node() == document().body();
  139. }