LayoutDocument.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <LibWeb/Dump.h>
  27. #include <LibWeb/Layout/LayoutDocument.h>
  28. #include <LibWeb/Layout/LayoutImage.h>
  29. #include <LibWeb/Layout/LayoutWidget.h>
  30. #include <LibWeb/Page/Frame.h>
  31. #include <LibWeb/Painting/StackingContext.h>
  32. namespace Web {
  33. LayoutDocument::LayoutDocument(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  34. : LayoutBlock(document, &document, move(style))
  35. {
  36. }
  37. LayoutDocument::~LayoutDocument()
  38. {
  39. }
  40. void LayoutDocument::build_stacking_context_tree()
  41. {
  42. if (stacking_context())
  43. return;
  44. set_stacking_context(make<StackingContext>(*this, nullptr));
  45. for_each_in_subtree_of_type<LayoutBox>([&](LayoutBox& box) {
  46. if (&box == this)
  47. return IterationDecision::Continue;
  48. if (!box.establishes_stacking_context()) {
  49. ASSERT(!box.stacking_context());
  50. return IterationDecision::Continue;
  51. }
  52. auto* parent_context = box.enclosing_stacking_context();
  53. ASSERT(parent_context);
  54. box.set_stacking_context(make<StackingContext>(box, parent_context));
  55. return IterationDecision::Continue;
  56. });
  57. }
  58. void LayoutDocument::did_set_viewport_rect(Badge<Frame>, const Gfx::IntRect& a_viewport_rect)
  59. {
  60. Gfx::FloatRect viewport_rect(a_viewport_rect.x(), a_viewport_rect.y(), a_viewport_rect.width(), a_viewport_rect.height());
  61. for_each_in_subtree_of_type<LayoutImage>([&](auto& layout_image) {
  62. const_cast<LayoutImage&>(layout_image).set_visible_in_viewport({}, viewport_rect.intersects(layout_image.absolute_rect()));
  63. return IterationDecision::Continue;
  64. });
  65. }
  66. void LayoutDocument::paint_all_phases(PaintContext& context)
  67. {
  68. paint(context, PaintPhase::Background);
  69. paint(context, PaintPhase::Border);
  70. paint(context, PaintPhase::Foreground);
  71. if (context.has_focus())
  72. paint(context, PaintPhase::FocusOutline);
  73. paint(context, PaintPhase::Overlay);
  74. }
  75. void LayoutDocument::paint(PaintContext& context, PaintPhase phase)
  76. {
  77. stacking_context()->paint(context, phase);
  78. }
  79. HitTestResult LayoutDocument::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  80. {
  81. return stacking_context()->hit_test(position, type);
  82. }
  83. void LayoutDocument::recompute_selection_states()
  84. {
  85. SelectionState state = SelectionState::None;
  86. auto selection = this->selection().normalized();
  87. for_each_in_subtree([&](auto& layout_node) {
  88. if (!selection.is_valid()) {
  89. // Everything gets SelectionState::None.
  90. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  91. state = SelectionState::StartAndEnd;
  92. } else if (&layout_node == selection.start().layout_node) {
  93. state = SelectionState::Start;
  94. } else if (&layout_node == selection.end().layout_node) {
  95. state = SelectionState::End;
  96. } else {
  97. if (state == SelectionState::Start)
  98. state = SelectionState::Full;
  99. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  100. state = SelectionState::None;
  101. }
  102. layout_node.set_selection_state(state);
  103. return IterationDecision::Continue;
  104. });
  105. }
  106. void LayoutDocument::set_selection(const LayoutRange& selection)
  107. {
  108. m_selection = selection;
  109. recompute_selection_states();
  110. }
  111. void LayoutDocument::set_selection_end(const LayoutPosition& position)
  112. {
  113. m_selection.set_end(position);
  114. recompute_selection_states();
  115. }
  116. }