BlockBox.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/InlineNode.h>
  33. #include <LibWeb/Layout/ReplacedBox.h>
  34. #include <LibWeb/Layout/TextNode.h>
  35. #include <LibWeb/Layout/WidgetBox.h>
  36. #include <math.h>
  37. namespace Web::Layout {
  38. BlockBox::BlockBox(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  39. : Box(document, node, move(style))
  40. {
  41. }
  42. BlockBox::~BlockBox()
  43. {
  44. }
  45. void BlockBox::paint(PaintContext& context, PaintPhase phase)
  46. {
  47. if (!is_visible())
  48. return;
  49. Box::paint(context, phase);
  50. // FIXME: Inline backgrounds etc.
  51. if (phase == PaintPhase::Foreground) {
  52. if (children_are_inline()) {
  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);
  58. }
  59. }
  60. }
  61. }
  62. if (phase == PaintPhase::FocusOutline) {
  63. if (children_are_inline()) {
  64. for (auto& line_box : m_line_boxes) {
  65. for (auto& fragment : line_box.fragments()) {
  66. auto* node = fragment.layout_node().dom_node();
  67. if (!node)
  68. continue;
  69. auto* parent = node->parent_element();
  70. if (!parent)
  71. continue;
  72. if (parent->is_focused())
  73. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  74. }
  75. }
  76. }
  77. }
  78. }
  79. HitTestResult BlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  80. {
  81. if (!children_are_inline())
  82. return Box::hit_test(position, type);
  83. HitTestResult last_good_candidate;
  84. for (auto& line_box : m_line_boxes) {
  85. for (auto& fragment : line_box.fragments()) {
  86. if (is<Box>(fragment.layout_node()) && downcast<Box>(fragment.layout_node()).stacking_context())
  87. continue;
  88. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  89. if (fragment.layout_node().is_block())
  90. return downcast<BlockBox>(fragment.layout_node()).hit_test(position, type);
  91. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  92. }
  93. if (fragment.absolute_rect().top() <= position.y())
  94. last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
  95. }
  96. }
  97. if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
  98. return last_good_candidate;
  99. return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  100. }
  101. void BlockBox::split_into_lines(BlockBox& container, LayoutMode layout_mode)
  102. {
  103. auto* line_box = &container.ensure_last_line_box();
  104. if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + width() > container.width()) {
  105. line_box = &container.add_line_box();
  106. }
  107. line_box->add_fragment(*this, 0, 0, width(), height());
  108. }
  109. }