BlockBox.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Painter.h>
  27. #include <LibWeb/CSS/StyleResolver.h>
  28. #include <LibWeb/DOM/Element.h>
  29. #include <LibWeb/Dump.h>
  30. #include <LibWeb/Layout/BlockBox.h>
  31. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  32. #include <LibWeb/Layout/InlineFormattingContext.h>
  33. #include <LibWeb/Layout/InlineNode.h>
  34. #include <LibWeb/Layout/ReplacedBox.h>
  35. #include <LibWeb/Layout/TextNode.h>
  36. #include <LibWeb/Layout/WidgetBox.h>
  37. #include <math.h>
  38. namespace Web::Layout {
  39. BlockBox::BlockBox(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  40. : Box(document, node, move(style))
  41. {
  42. }
  43. BlockBox::~BlockBox()
  44. {
  45. }
  46. void BlockBox::paint(PaintContext& context, PaintPhase phase)
  47. {
  48. if (!is_visible())
  49. return;
  50. Box::paint(context, phase);
  51. if (!children_are_inline())
  52. return;
  53. for (auto& line_box : m_line_boxes) {
  54. for (auto& fragment : line_box.fragments()) {
  55. if (context.should_show_line_box_borders())
  56. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  57. fragment.paint(context, phase);
  58. }
  59. }
  60. // FIXME: Merge this loop with the above somehow..
  61. if (phase == PaintPhase::FocusOutline) {
  62. for (auto& line_box : m_line_boxes) {
  63. for (auto& fragment : line_box.fragments()) {
  64. auto* node = fragment.layout_node().dom_node();
  65. if (!node)
  66. continue;
  67. auto* parent = node->parent_element();
  68. if (!parent)
  69. continue;
  70. if (parent->is_focused())
  71. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  72. }
  73. }
  74. }
  75. }
  76. HitTestResult BlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  77. {
  78. if (!children_are_inline())
  79. return Box::hit_test(position, type);
  80. HitTestResult last_good_candidate;
  81. for (auto& line_box : m_line_boxes) {
  82. for (auto& fragment : line_box.fragments()) {
  83. if (is<Box>(fragment.layout_node()) && downcast<Box>(fragment.layout_node()).stacking_context())
  84. continue;
  85. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  86. if (fragment.layout_node().is_block())
  87. return downcast<BlockBox>(fragment.layout_node()).hit_test(position, type);
  88. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  89. }
  90. if (fragment.absolute_rect().top() <= position.y())
  91. last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
  92. }
  93. }
  94. if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
  95. return last_good_candidate;
  96. return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  97. }
  98. void BlockBox::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  99. {
  100. auto& containing_block = context.containing_block();
  101. auto* line_box = &containing_block.ensure_last_line_box();
  102. context.dimension_box_on_line(*this, layout_mode);
  103. float available_width = context.available_width_at_line(containing_block.line_boxes().size() - 1);
  104. if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + border_box_width() > available_width) {
  105. line_box = &containing_block.add_line_box();
  106. }
  107. line_box->add_fragment(*this, 0, 0, border_box_width(), height());
  108. }
  109. }