InitialContainingBlockBox.cpp 5.1 KB

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