LayoutBox.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. Rect padded_rect;
  21. padded_rect.set_x(x() - box_model().padding().left.to_px());
  22. padded_rect.set_width(width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  23. padded_rect.set_y(y() - box_model().padding().top.to_px());
  24. padded_rect.set_height(height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  25. if (!is_body()) {
  26. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  27. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  28. context.painter().fill_rect(padded_rect, bgcolor.value()->to_color(document()));
  29. }
  30. auto bgimage = style().property(CSS::PropertyID::BackgroundImage);
  31. if (bgimage.has_value() && bgimage.value()->is_image()) {
  32. auto& image_value = static_cast<const ImageStyleValue&>(*bgimage.value());
  33. if (image_value.bitmap()) {
  34. context.painter().draw_tiled_bitmap(padded_rect, *image_value.bitmap());
  35. }
  36. }
  37. }
  38. // FIXME: Respect all individual border sides
  39. auto border_width_value = style().property(CSS::PropertyID::BorderTopWidth);
  40. auto border_color_value = style().property(CSS::PropertyID::BorderTopColor);
  41. auto border_style_value = style().property(CSS::PropertyID::BorderTopStyle);
  42. if (border_width_value.has_value() && border_color_value.has_value()) {
  43. int border_width = border_width_value.value()->to_length().to_px();
  44. Color border_color = border_color_value.value()->to_color(document());
  45. if (border_style_value.has_value() && border_style_value.value()->to_string() == "inset") {
  46. // border-style: inset
  47. auto shadow_color = Color::from_rgb(0x888888);
  48. auto highlight_color = Color::from_rgb(0x5a5a5a);
  49. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), highlight_color, border_width);
  50. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), shadow_color, border_width);
  51. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), shadow_color, border_width);
  52. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), highlight_color, border_width);
  53. } else if (border_style_value.has_value() && border_style_value.value()->to_string() == "outset") {
  54. // border-style: outset
  55. auto highlight_color = Color::from_rgb(0x888888);
  56. auto shadow_color = Color::from_rgb(0x5a5a5a);
  57. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), highlight_color, border_width);
  58. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), shadow_color, border_width);
  59. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), shadow_color, border_width);
  60. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), highlight_color, border_width);
  61. } else {
  62. // border-style: solid
  63. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), border_color, border_width);
  64. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), border_color, border_width);
  65. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), border_color, border_width);
  66. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), border_color, border_width);
  67. }
  68. }
  69. LayoutNodeWithStyleAndBoxModelMetrics::render(context);
  70. }
  71. HitTestResult LayoutBox::hit_test(const Point& position) const
  72. {
  73. // FIXME: It would be nice if we could confidently skip over hit testing
  74. // parts of the layout tree, but currently we can't just check
  75. // m_rect.contains() since inline text rects can't be trusted..
  76. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  77. for_each_child([&](auto& child) {
  78. auto child_result = child.hit_test(position);
  79. if (child_result.layout_node)
  80. result = child_result;
  81. });
  82. return result;
  83. }
  84. void LayoutBox::set_needs_display()
  85. {
  86. auto* frame = document().frame();
  87. ASSERT(frame);
  88. if (!is_inline()) {
  89. const_cast<Frame*>(frame)->set_needs_display(rect());
  90. return;
  91. }
  92. LayoutNode::set_needs_display();
  93. }
  94. bool LayoutBox::is_body() const
  95. {
  96. return node() && node() == document().body();
  97. }