InitialContainingBlock.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. set_stacking_context(make<StackingContext>(*this, nullptr));
  22. for_each_in_inclusive_subtree_of_type<Box>([&](Box& box) {
  23. if (&box == this)
  24. return IterationDecision::Continue;
  25. if (!box.establishes_stacking_context()) {
  26. VERIFY(!box.stacking_context());
  27. return IterationDecision::Continue;
  28. }
  29. auto* parent_context = box.enclosing_stacking_context();
  30. VERIFY(parent_context);
  31. box.set_stacking_context(make<StackingContext>(box, parent_context));
  32. return IterationDecision::Continue;
  33. });
  34. }
  35. void InitialContainingBlock::paint_all_phases(PaintContext& context)
  36. {
  37. context.painter().fill_rect(enclosing_int_rect(absolute_rect()), context.palette().base());
  38. context.painter().translate(-context.viewport_rect().location());
  39. stacking_context()->paint(context);
  40. }
  41. HitTestResult InitialContainingBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  42. {
  43. return stacking_context()->hit_test(position, type);
  44. }
  45. void InitialContainingBlock::recompute_selection_states()
  46. {
  47. SelectionState state = SelectionState::None;
  48. auto selection = this->selection().normalized();
  49. for_each_in_inclusive_subtree([&](auto& layout_node) {
  50. if (!selection.is_valid()) {
  51. // Everything gets SelectionState::None.
  52. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  53. state = SelectionState::StartAndEnd;
  54. } else if (&layout_node == selection.start().layout_node) {
  55. state = SelectionState::Start;
  56. } else if (&layout_node == selection.end().layout_node) {
  57. state = SelectionState::End;
  58. } else {
  59. if (state == SelectionState::Start)
  60. state = SelectionState::Full;
  61. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  62. state = SelectionState::None;
  63. }
  64. layout_node.set_selection_state(state);
  65. return IterationDecision::Continue;
  66. });
  67. }
  68. void InitialContainingBlock::set_selection(const LayoutRange& selection)
  69. {
  70. m_selection = selection;
  71. recompute_selection_states();
  72. }
  73. void InitialContainingBlock::set_selection_end(const LayoutPosition& position)
  74. {
  75. m_selection.set_end(position);
  76. recompute_selection_states();
  77. }
  78. }