InitialContainingBlock.cpp 3.1 KB

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