InitialContainingBlock.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2018-2022, 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/Paintable.h>
  11. #include <LibWeb/Painting/StackingContext.h>
  12. namespace Web::Layout {
  13. InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  14. : BlockContainer(document, &document, move(style))
  15. {
  16. }
  17. InitialContainingBlock::~InitialContainingBlock()
  18. {
  19. }
  20. void InitialContainingBlock::build_stacking_context_tree()
  21. {
  22. m_paint_box->set_stacking_context(make<Painting::StackingContext>(*this, nullptr));
  23. for_each_in_inclusive_subtree_of_type<Box>([&](Box& box) {
  24. if (&box == this)
  25. return IterationDecision::Continue;
  26. if (!box.establishes_stacking_context()) {
  27. VERIFY(!box.m_paint_box->stacking_context());
  28. return IterationDecision::Continue;
  29. }
  30. auto* parent_context = box.m_paint_box->enclosing_stacking_context();
  31. VERIFY(parent_context);
  32. box.m_paint_box->set_stacking_context(make<Painting::StackingContext>(box, parent_context));
  33. return IterationDecision::Continue;
  34. });
  35. }
  36. void InitialContainingBlock::paint_all_phases(PaintContext& context)
  37. {
  38. context.painter().fill_rect(enclosing_int_rect(m_paint_box->absolute_rect()), context.palette().base());
  39. context.painter().translate(-context.viewport_rect().location());
  40. m_paint_box->stacking_context()->paint(context);
  41. }
  42. HitTestResult InitialContainingBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  43. {
  44. return m_paint_box->stacking_context()->hit_test(position, type);
  45. }
  46. void InitialContainingBlock::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 InitialContainingBlock::set_selection(const LayoutRange& selection)
  70. {
  71. m_selection = selection;
  72. recompute_selection_states();
  73. }
  74. void InitialContainingBlock::set_selection_end(const LayoutPosition& position)
  75. {
  76. m_selection.set_end(position);
  77. recompute_selection_states();
  78. }
  79. }