InitialContainingBlock.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2018-2021, 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/HTML/BrowsingContext.h>
  9. #include <LibWeb/Layout/InitialContainingBlock.h>
  10. #include <LibWeb/Painting/StackingContext.h>
  11. namespace Web::Layout {
  12. InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  13. : BlockContainer(document, &document, move(style))
  14. {
  15. }
  16. InitialContainingBlock::~InitialContainingBlock()
  17. {
  18. }
  19. void InitialContainingBlock::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 InitialContainingBlock::paint_all_phases(PaintContext& context)
  38. {
  39. context.painter().fill_rect(enclosing_int_rect(absolute_rect()), context.palette().base());
  40. context.painter().translate(-context.viewport_rect().location());
  41. stacking_context()->paint(context);
  42. }
  43. HitTestResult InitialContainingBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  44. {
  45. return stacking_context()->hit_test(position, type);
  46. }
  47. void InitialContainingBlock::recompute_selection_states()
  48. {
  49. SelectionState state = SelectionState::None;
  50. auto selection = this->selection().normalized();
  51. for_each_in_inclusive_subtree([&](auto& layout_node) {
  52. if (!selection.is_valid()) {
  53. // Everything gets SelectionState::None.
  54. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  55. state = SelectionState::StartAndEnd;
  56. } else if (&layout_node == selection.start().layout_node) {
  57. state = SelectionState::Start;
  58. } else if (&layout_node == selection.end().layout_node) {
  59. state = SelectionState::End;
  60. } else {
  61. if (state == SelectionState::Start)
  62. state = SelectionState::Full;
  63. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  64. state = SelectionState::None;
  65. }
  66. layout_node.set_selection_state(state);
  67. return IterationDecision::Continue;
  68. });
  69. }
  70. void InitialContainingBlock::set_selection(const LayoutRange& selection)
  71. {
  72. m_selection = selection;
  73. recompute_selection_states();
  74. }
  75. void InitialContainingBlock::set_selection_end(const LayoutPosition& position)
  76. {
  77. m_selection.set_end(position);
  78. recompute_selection_states();
  79. }
  80. }