BlockBox.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Painter.h>
  7. #include <LibWeb/CSS/StyleResolver.h>
  8. #include <LibWeb/Dump.h>
  9. #include <LibWeb/Layout/BlockBox.h>
  10. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  11. #include <LibWeb/Layout/InlineFormattingContext.h>
  12. #include <LibWeb/Layout/ReplacedBox.h>
  13. #include <LibWeb/Layout/TextNode.h>
  14. namespace Web::Layout {
  15. BlockBox::BlockBox(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  16. : Box(document, node, move(style))
  17. {
  18. }
  19. BlockBox::BlockBox(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  20. : Box(document, node, move(computed_values))
  21. {
  22. }
  23. BlockBox::~BlockBox()
  24. {
  25. }
  26. bool BlockBox::should_clip_overflow() const
  27. {
  28. return computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  29. }
  30. void BlockBox::paint(PaintContext& context, 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(padded_rect()));
  41. context.painter().translate(-m_scroll_offset.to_type<int>());
  42. }
  43. for (auto& line_box : m_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. 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 == PaintPhase::FocusOutline) {
  55. for (auto& line_box : m_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 BlockBox::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 : m_line_boxes) {
  75. for (auto& fragment : line_box.fragments()) {
  76. if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).stacking_context())
  77. continue;
  78. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  79. if (is<BlockBox>(fragment.layout_node()))
  80. return verify_cast<BlockBox>(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 { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  90. }
  91. void BlockBox::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  92. {
  93. auto& containing_block = context.containing_block();
  94. auto* line_box = &containing_block.ensure_last_line_box();
  95. context.dimension_box_on_line(*this, layout_mode);
  96. float available_width = context.available_width_at_line(containing_block.line_boxes().size() - 1);
  97. if (layout_mode == LayoutMode::AllPossibleLineBreaks && line_box->width() > 0) {
  98. line_box = &containing_block.add_line_box();
  99. } else if (layout_mode == LayoutMode::Default && line_box->width() > 0 && line_box->width() + border_box_width() > available_width) {
  100. line_box = &containing_block.add_line_box();
  101. }
  102. line_box->add_fragment(*this, 0, 0, border_box_width(), height());
  103. }
  104. bool BlockBox::is_scrollable() const
  105. {
  106. // FIXME: Support horizontal scroll as well (overflow-x)
  107. return computed_values().overflow_y() == CSS::Overflow::Scroll;
  108. }
  109. void BlockBox::set_scroll_offset(const Gfx::FloatPoint& offset)
  110. {
  111. // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
  112. if (offset.y() < 0 || m_scroll_offset == offset)
  113. return;
  114. m_scroll_offset = offset;
  115. set_needs_display();
  116. }
  117. bool BlockBox::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned int, unsigned int, int wheel_delta)
  118. {
  119. if (!is_scrollable())
  120. return false;
  121. auto new_offset = m_scroll_offset;
  122. new_offset.translate_by(0, wheel_delta);
  123. set_scroll_offset(new_offset);
  124. return true;
  125. }
  126. }