InitialContainingBlockBox.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Painter.h>
  7. #include <LibWeb/Dump.h>
  8. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  9. #include <LibWeb/Page/Frame.h>
  10. #include <LibWeb/Painting/StackingContext.h>
  11. namespace Web::Layout {
  12. InitialContainingBlockBox::InitialContainingBlockBox(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  13. : BlockBox(document, &document, move(style))
  14. {
  15. }
  16. InitialContainingBlockBox::~InitialContainingBlockBox()
  17. {
  18. }
  19. void InitialContainingBlockBox::build_stacking_context_tree()
  20. {
  21. if (stacking_context())
  22. return;
  23. set_stacking_context(make<StackingContext>(*this, nullptr));
  24. for_each_in_inclusive_subtree_of_type<Box>([&](Box& box) {
  25. if (&box == this)
  26. return IterationDecision::Continue;
  27. if (!box.establishes_stacking_context()) {
  28. VERIFY(!box.stacking_context());
  29. return IterationDecision::Continue;
  30. }
  31. auto* parent_context = box.enclosing_stacking_context();
  32. VERIFY(parent_context);
  33. box.set_stacking_context(make<StackingContext>(box, parent_context));
  34. return IterationDecision::Continue;
  35. });
  36. }
  37. void InitialContainingBlockBox::paint_all_phases(PaintContext& context)
  38. {
  39. context.painter().translate(-context.viewport_rect().location());
  40. stacking_context()->paint(context);
  41. }
  42. HitTestResult InitialContainingBlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  43. {
  44. return stacking_context()->hit_test(position, type);
  45. }
  46. void InitialContainingBlockBox::recompute_selection_states()
  47. {
  48. SelectionState state = SelectionState::None;
  49. auto selection = this->selection().normalized();
  50. for_each_in_inclusive_subtree([&](auto& layout_node) {
  51. if (!selection.is_valid()) {
  52. // Everything gets SelectionState::None.
  53. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  54. state = SelectionState::StartAndEnd;
  55. } else if (&layout_node == selection.start().layout_node) {
  56. state = SelectionState::Start;
  57. } else if (&layout_node == selection.end().layout_node) {
  58. state = SelectionState::End;
  59. } else {
  60. if (state == SelectionState::Start)
  61. state = SelectionState::Full;
  62. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  63. state = SelectionState::None;
  64. }
  65. layout_node.set_selection_state(state);
  66. return IterationDecision::Continue;
  67. });
  68. }
  69. void InitialContainingBlockBox::set_selection(const LayoutRange& selection)
  70. {
  71. m_selection = selection;
  72. recompute_selection_states();
  73. }
  74. void InitialContainingBlockBox::set_selection_end(const LayoutPosition& position)
  75. {
  76. m_selection.set_end(position);
  77. recompute_selection_states();
  78. }
  79. }