LayoutBox.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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::render(RenderingContext& context)
  10. {
  11. if (!is_visible())
  12. return;
  13. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  14. context.painter().draw_rect(m_rect, Color::Blue);
  15. #endif
  16. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  17. if (!is_anonymous() && node() == document().hovered_node())
  18. context.painter().draw_rect(m_rect, Color::Red);
  19. #endif
  20. if (node() && document().inspected_node() == node())
  21. context.painter().draw_rect(m_rect, Color::Magenta);
  22. Rect padded_rect;
  23. padded_rect.set_x(x() - box_model().padding().left.to_px());
  24. padded_rect.set_width(width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  25. padded_rect.set_y(y() - box_model().padding().top.to_px());
  26. padded_rect.set_height(height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  27. if (!is_body()) {
  28. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  29. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  30. context.painter().fill_rect(padded_rect, bgcolor.value()->to_color(document()));
  31. }
  32. auto bgimage = style().property(CSS::PropertyID::BackgroundImage);
  33. if (bgimage.has_value() && bgimage.value()->is_image()) {
  34. auto& image_value = static_cast<const ImageStyleValue&>(*bgimage.value());
  35. if (image_value.bitmap()) {
  36. context.painter().draw_tiled_bitmap(padded_rect, *image_value.bitmap());
  37. }
  38. }
  39. }
  40. // FIXME: Respect all individual border sides
  41. auto border_width_value = style().property(CSS::PropertyID::BorderTopWidth);
  42. auto border_color_value = style().property(CSS::PropertyID::BorderTopColor);
  43. auto border_style_value = style().property(CSS::PropertyID::BorderTopStyle);
  44. if (border_width_value.has_value()) {
  45. int border_width = border_width_value.value()->to_length().to_px();
  46. Color border_color;
  47. if (border_color_value.has_value())
  48. border_color = border_color_value.value()->to_color(document());
  49. else {
  50. // FIXME: This is basically CSS "currentColor" which should be handled elsewhere
  51. // in a much more reusable way.
  52. auto color_value = style().property(CSS::PropertyID::Color);
  53. if (color_value.has_value())
  54. border_color = color_value.value()->to_color(document());
  55. else
  56. border_color = Color::Black;
  57. }
  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. LayoutNodeWithStyleAndBoxModelMetrics::render(context);
  83. }
  84. HitTestResult LayoutBox::hit_test(const Point& position) const
  85. {
  86. // FIXME: It would be nice if we could confidently skip over hit testing
  87. // parts of the layout tree, but currently we can't just check
  88. // m_rect.contains() since inline text rects can't be trusted..
  89. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  90. for_each_child([&](auto& child) {
  91. auto child_result = child.hit_test(position);
  92. if (child_result.layout_node)
  93. result = child_result;
  94. });
  95. return result;
  96. }
  97. void LayoutBox::set_needs_display()
  98. {
  99. auto* frame = document().frame();
  100. ASSERT(frame);
  101. if (!is_inline()) {
  102. const_cast<Frame*>(frame)->set_needs_display(rect());
  103. return;
  104. }
  105. LayoutNode::set_needs_display();
  106. }
  107. bool LayoutBox::is_body() const
  108. {
  109. return node() && node() == document().body();
  110. }