LayoutBlock.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutDocument.h>
  32. #include <LibWeb/Layout/LayoutInline.h>
  33. #include <LibWeb/Layout/LayoutReplaced.h>
  34. #include <LibWeb/Layout/LayoutText.h>
  35. #include <LibWeb/Layout/LayoutWidget.h>
  36. #include <math.h>
  37. namespace Web {
  38. LayoutBlock::LayoutBlock(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  39. : LayoutBox(document, node, move(style))
  40. {
  41. }
  42. LayoutBlock::~LayoutBlock()
  43. {
  44. }
  45. LayoutNode& LayoutBlock::inline_wrapper()
  46. {
  47. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  48. append_child(adopt(*new LayoutBlock(document(), nullptr, style_for_anonymous_block())));
  49. last_child()->set_children_are_inline(true);
  50. }
  51. return *last_child();
  52. }
  53. void LayoutBlock::paint(PaintContext& context, PaintPhase phase)
  54. {
  55. if (!is_visible())
  56. return;
  57. LayoutBox::paint(context, phase);
  58. // FIXME: Inline backgrounds etc.
  59. if (phase == PaintPhase::Foreground) {
  60. if (children_are_inline()) {
  61. for (auto& line_box : m_line_boxes) {
  62. for (auto& fragment : line_box.fragments()) {
  63. if (context.should_show_line_box_borders())
  64. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  65. fragment.paint(context);
  66. }
  67. }
  68. }
  69. }
  70. if (phase == PaintPhase::FocusOutline) {
  71. if (children_are_inline()) {
  72. for (auto& line_box : m_line_boxes) {
  73. for (auto& fragment : line_box.fragments()) {
  74. auto* node = fragment.layout_node().node();
  75. if (!node)
  76. continue;
  77. auto* parent = node->parent_element();
  78. if (!parent)
  79. continue;
  80. if (parent->is_focused())
  81. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  82. }
  83. }
  84. }
  85. }
  86. }
  87. HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  88. {
  89. if (!children_are_inline())
  90. return LayoutBox::hit_test(position, type);
  91. HitTestResult last_good_candidate;
  92. for (auto& line_box : m_line_boxes) {
  93. for (auto& fragment : line_box.fragments()) {
  94. if (is<LayoutBox>(fragment.layout_node()) && downcast<LayoutBox>(fragment.layout_node()).stacking_context())
  95. continue;
  96. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  97. if (fragment.layout_node().is_block())
  98. return downcast<LayoutBlock>(fragment.layout_node()).hit_test(position, type);
  99. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  100. }
  101. if (fragment.absolute_rect().top() <= position.y())
  102. last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
  103. }
  104. }
  105. if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
  106. return last_good_candidate;
  107. return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  108. }
  109. NonnullRefPtr<CSS::StyleProperties> LayoutBlock::style_for_anonymous_block() const
  110. {
  111. auto new_style = CSS::StyleProperties::create();
  112. specified_style().for_each_property([&](auto property_id, auto& value) {
  113. if (CSS::StyleResolver::is_inherited_property(property_id))
  114. new_style->set_property(property_id, value);
  115. });
  116. return new_style;
  117. }
  118. void LayoutBlock::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  119. {
  120. auto* line_box = &container.ensure_last_line_box();
  121. if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + width() > container.width()) {
  122. line_box = &container.add_line_box();
  123. }
  124. line_box->add_fragment(*this, 0, 0, width(), height());
  125. }
  126. }