BlockContainer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Painter.h>
  7. #include <LibWeb/Dump.h>
  8. #include <LibWeb/Layout/BlockContainer.h>
  9. #include <LibWeb/Layout/InitialContainingBlock.h>
  10. #include <LibWeb/Layout/InlineFormattingContext.h>
  11. #include <LibWeb/Layout/ReplacedBox.h>
  12. #include <LibWeb/Layout/TextNode.h>
  13. #include <LibWeb/Painting/Box.h>
  14. namespace Web::Layout {
  15. BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  16. : Box(document, node, move(style))
  17. {
  18. }
  19. BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  20. : Box(document, node, move(computed_values))
  21. {
  22. }
  23. BlockContainer::~BlockContainer()
  24. {
  25. }
  26. bool BlockContainer::should_clip_overflow() const
  27. {
  28. return computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  29. }
  30. void BlockContainer::paint(PaintContext& context, Painting::PaintPhase phase)
  31. {
  32. if (!is_visible())
  33. return;
  34. Box::paint(context, phase);
  35. if (!children_are_inline())
  36. return;
  37. if (should_clip_overflow()) {
  38. context.painter().save();
  39. // FIXME: Handle overflow-x and overflow-y being different values.
  40. context.painter().add_clip_rect(enclosing_int_rect(m_paint_box->absolute_padding_box_rect()));
  41. context.painter().translate(-m_scroll_offset.to_type<int>());
  42. }
  43. for (auto& line_box : paint_box()->line_boxes()) {
  44. for (auto& fragment : line_box.fragments()) {
  45. if (context.should_show_line_box_borders())
  46. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  47. const_cast<LineBoxFragment&>(fragment).paint(context, phase);
  48. }
  49. }
  50. if (should_clip_overflow()) {
  51. context.painter().restore();
  52. }
  53. // FIXME: Merge this loop with the above somehow..
  54. if (phase == Painting::PaintPhase::FocusOutline) {
  55. for (auto& line_box : paint_box()->line_boxes()) {
  56. for (auto& fragment : line_box.fragments()) {
  57. auto* node = fragment.layout_node().dom_node();
  58. if (!node)
  59. continue;
  60. auto* parent = node->parent_element();
  61. if (!parent)
  62. continue;
  63. if (parent->is_focused())
  64. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  65. }
  66. }
  67. }
  68. }
  69. HitTestResult BlockContainer::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  70. {
  71. if (!children_are_inline())
  72. return Box::hit_test(position, type);
  73. HitTestResult last_good_candidate;
  74. for (auto& line_box : paint_box()->line_boxes()) {
  75. for (auto& fragment : line_box.fragments()) {
  76. if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).m_paint_box->stacking_context())
  77. continue;
  78. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  79. if (is<BlockContainer>(fragment.layout_node()))
  80. return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type);
  81. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  82. }
  83. if (fragment.absolute_rect().top() <= position.y())
  84. last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
  85. }
  86. }
  87. if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
  88. return last_good_candidate;
  89. return { m_paint_box->absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
  90. }
  91. bool BlockContainer::is_scrollable() const
  92. {
  93. // FIXME: Support horizontal scroll as well (overflow-x)
  94. return computed_values().overflow_y() == CSS::Overflow::Scroll;
  95. }
  96. void BlockContainer::set_scroll_offset(const Gfx::FloatPoint& offset)
  97. {
  98. // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
  99. if (offset.y() < 0 || m_scroll_offset == offset)
  100. return;
  101. m_scroll_offset = offset;
  102. set_needs_display();
  103. }
  104. bool BlockContainer::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned int, unsigned int, int wheel_delta_x, int wheel_delta_y)
  105. {
  106. if (!is_scrollable())
  107. return false;
  108. auto new_offset = m_scroll_offset;
  109. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  110. set_scroll_offset(new_offset);
  111. return true;
  112. }
  113. Painting::BoxWithLines const* BlockContainer::paint_box() const
  114. {
  115. return static_cast<Painting::BoxWithLines const*>(Box::paint_box());
  116. }
  117. }