BlockBox.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if (!children_are_inline())
  51. return;
  52. for (auto& line_box : m_line_boxes) {
  53. for (auto& fragment : line_box.fragments()) {
  54. if (context.should_show_line_box_borders())
  55. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  56. fragment.paint(context, phase);
  57. }
  58. }
  59. // FIXME: Merge this loop with the above somehow..
  60. if (phase == PaintPhase::FocusOutline) {
  61. for (auto& line_box : m_line_boxes) {
  62. for (auto& fragment : line_box.fragments()) {
  63. auto* node = fragment.layout_node().dom_node();
  64. if (!node)
  65. continue;
  66. auto* parent = node->parent_element();
  67. if (!parent)
  68. continue;
  69. if (parent->is_focused())
  70. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  71. }
  72. }
  73. }
  74. }
  75. HitTestResult BlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  76. {
  77. if (!children_are_inline())
  78. return Box::hit_test(position, type);
  79. HitTestResult last_good_candidate;
  80. for (auto& line_box : m_line_boxes) {
  81. for (auto& fragment : line_box.fragments()) {
  82. if (is<Box>(fragment.layout_node()) && downcast<Box>(fragment.layout_node()).stacking_context())
  83. continue;
  84. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  85. if (fragment.layout_node().is_block())
  86. return downcast<BlockBox>(fragment.layout_node()).hit_test(position, type);
  87. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  88. }
  89. if (fragment.absolute_rect().top() <= position.y())
  90. last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
  91. }
  92. }
  93. if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
  94. return last_good_candidate;
  95. return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  96. }
  97. void BlockBox::split_into_lines(BlockBox& container, LayoutMode layout_mode)
  98. {
  99. auto* line_box = &container.ensure_last_line_box();
  100. if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + width() > container.width()) {
  101. line_box = &container.add_line_box();
  102. }
  103. line_box->add_fragment(*this, 0, 0, width(), height());
  104. }
  105. }