InitialContainingBlockBox.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. int painted_image_width = 0;
  63. int painted_image_height = 0;
  64. switch (document().background_repeat()) {
  65. case CSS::Repeat::Repeat:
  66. painted_image_width = context.viewport_rect().x() + context.viewport_rect().width();
  67. painted_image_height = context.viewport_rect().y() + context.viewport_rect().height();
  68. break;
  69. case CSS::Repeat::RepeatX:
  70. painted_image_width = context.viewport_rect().x() + context.viewport_rect().width();
  71. painted_image_height = background_bitmap->rect().height();
  72. break;
  73. case CSS::Repeat::RepeatY:
  74. painted_image_width = background_bitmap->rect().width();
  75. painted_image_height = context.viewport_rect().y() + context.viewport_rect().height();
  76. break;
  77. case CSS::Repeat::NoRepeat:
  78. default: // FIXME: Support 'round' and 'square'
  79. painted_image_width = background_bitmap->rect().width();
  80. painted_image_height = background_bitmap->rect().height();
  81. break;
  82. }
  83. Gfx::IntRect background_rect { 0, 0, painted_image_width, painted_image_height };
  84. context.painter().blit_tiled(background_rect, *background_bitmap, background_bitmap->rect());
  85. }
  86. }
  87. void InitialContainingBlockBox::paint_all_phases(PaintContext& context)
  88. {
  89. paint_document_background(context);
  90. paint(context, PaintPhase::Background);
  91. paint(context, PaintPhase::Border);
  92. paint(context, PaintPhase::Foreground);
  93. if (context.has_focus())
  94. paint(context, PaintPhase::FocusOutline);
  95. paint(context, PaintPhase::Overlay);
  96. }
  97. void InitialContainingBlockBox::paint(PaintContext& context, PaintPhase phase)
  98. {
  99. stacking_context()->paint(context, phase);
  100. }
  101. HitTestResult InitialContainingBlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  102. {
  103. return stacking_context()->hit_test(position, type);
  104. }
  105. void InitialContainingBlockBox::recompute_selection_states()
  106. {
  107. SelectionState state = SelectionState::None;
  108. auto selection = this->selection().normalized();
  109. for_each_in_subtree([&](auto& layout_node) {
  110. if (!selection.is_valid()) {
  111. // Everything gets SelectionState::None.
  112. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  113. state = SelectionState::StartAndEnd;
  114. } else if (&layout_node == selection.start().layout_node) {
  115. state = SelectionState::Start;
  116. } else if (&layout_node == selection.end().layout_node) {
  117. state = SelectionState::End;
  118. } else {
  119. if (state == SelectionState::Start)
  120. state = SelectionState::Full;
  121. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  122. state = SelectionState::None;
  123. }
  124. layout_node.set_selection_state(state);
  125. return IterationDecision::Continue;
  126. });
  127. }
  128. void InitialContainingBlockBox::set_selection(const LayoutRange& selection)
  129. {
  130. m_selection = selection;
  131. recompute_selection_states();
  132. }
  133. void InitialContainingBlockBox::set_selection_end(const LayoutPosition& position)
  134. {
  135. m_selection.set_end(position);
  136. recompute_selection_states();
  137. }
  138. }