InitialContainingBlock.cpp 3.0 KB

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