BlockContainer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2018-2021, 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. namespace Web::Layout {
  14. BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  15. : Box(document, node, move(style))
  16. {
  17. }
  18. BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  19. : Box(document, node, move(computed_values))
  20. {
  21. }
  22. BlockContainer::~BlockContainer()
  23. {
  24. }
  25. bool BlockContainer::should_clip_overflow() const
  26. {
  27. return computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  28. }
  29. void BlockContainer::paint(PaintContext& context, PaintPhase phase)
  30. {
  31. if (!is_visible())
  32. return;
  33. Box::paint(context, phase);
  34. if (!children_are_inline())
  35. return;
  36. if (should_clip_overflow()) {
  37. context.painter().save();
  38. // FIXME: Handle overflow-x and overflow-y being different values.
  39. context.painter().add_clip_rect(enclosing_int_rect(padded_rect()));
  40. context.painter().translate(-m_scroll_offset.to_type<int>());
  41. }
  42. for (auto& line_box : m_line_boxes) {
  43. for (auto& fragment : line_box.fragments()) {
  44. if (context.should_show_line_box_borders())
  45. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  46. fragment.paint(context, phase);
  47. }
  48. }
  49. if (should_clip_overflow()) {
  50. context.painter().restore();
  51. }
  52. // FIXME: Merge this loop with the above somehow..
  53. if (phase == PaintPhase::FocusOutline) {
  54. for (auto& line_box : m_line_boxes) {
  55. for (auto& fragment : line_box.fragments()) {
  56. auto* node = fragment.layout_node().dom_node();
  57. if (!node)
  58. continue;
  59. auto* parent = node->parent_element();
  60. if (!parent)
  61. continue;
  62. if (parent->is_focused())
  63. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  64. }
  65. }
  66. }
  67. }
  68. HitTestResult BlockContainer::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  69. {
  70. if (!children_are_inline())
  71. return Box::hit_test(position, type);
  72. HitTestResult last_good_candidate;
  73. for (auto& line_box : m_line_boxes) {
  74. for (auto& fragment : line_box.fragments()) {
  75. if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).stacking_context())
  76. continue;
  77. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  78. if (is<BlockContainer>(fragment.layout_node()))
  79. return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type);
  80. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  81. }
  82. if (fragment.absolute_rect().top() <= position.y())
  83. last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
  84. }
  85. }
  86. if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
  87. return last_good_candidate;
  88. return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  89. }
  90. void BlockContainer::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  91. {
  92. auto& containing_block = context.containing_block();
  93. auto* line_box = &containing_block.ensure_last_line_box();
  94. context.dimension_box_on_line(*this, layout_mode);
  95. float available_width = context.available_width_at_line(containing_block.line_boxes().size() - 1);
  96. if (layout_mode == LayoutMode::AllPossibleLineBreaks && line_box->width() > 0) {
  97. line_box = &containing_block.add_line_box();
  98. } else if (layout_mode == LayoutMode::Default && line_box->width() > 0 && line_box->width() + border_box_width() > available_width) {
  99. line_box = &containing_block.add_line_box();
  100. }
  101. line_box->add_fragment(*this, 0, 0, border_box_width(), height());
  102. }
  103. bool BlockContainer::is_scrollable() const
  104. {
  105. // FIXME: Support horizontal scroll as well (overflow-x)
  106. return computed_values().overflow_y() == CSS::Overflow::Scroll;
  107. }
  108. void BlockContainer::set_scroll_offset(const Gfx::FloatPoint& offset)
  109. {
  110. // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
  111. if (offset.y() < 0 || m_scroll_offset == offset)
  112. return;
  113. m_scroll_offset = offset;
  114. set_needs_display();
  115. }
  116. bool BlockContainer::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned int, unsigned int, int wheel_delta)
  117. {
  118. if (!is_scrollable())
  119. return false;
  120. auto new_offset = m_scroll_offset;
  121. new_offset.translate_by(0, wheel_delta);
  122. set_scroll_offset(new_offset);
  123. return true;
  124. }
  125. }