InitialContainingBlockBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, 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/Layout/InitialContainingBlockBox.h>
  9. #include <LibWeb/Page/Frame.h>
  10. #include <LibWeb/Painting/StackingContext.h>
  11. namespace Web::Layout {
  12. InitialContainingBlockBox::InitialContainingBlockBox(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  13. : BlockBox(document, &document, move(style))
  14. {
  15. }
  16. InitialContainingBlockBox::~InitialContainingBlockBox()
  17. {
  18. }
  19. void InitialContainingBlockBox::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 InitialContainingBlockBox::paint_document_background(PaintContext& context)
  38. {
  39. context.painter().fill_rect(Gfx::IntRect { {}, context.viewport_rect().size() }, document().background_color(context.palette()));
  40. context.painter().translate(-context.viewport_rect().location());
  41. if (auto background_bitmap = document().background_image()) {
  42. Gfx::IntRect background_rect = { 0, 0, context.viewport_rect().x() + context.viewport_rect().width(), context.viewport_rect().y() + context.viewport_rect().height() };
  43. paint_background_image(context, *background_bitmap, document().background_repeat_x(), document().background_repeat_y(), move(background_rect));
  44. }
  45. }
  46. void InitialContainingBlockBox::paint_all_phases(PaintContext& context)
  47. {
  48. paint_document_background(context);
  49. paint(context, PaintPhase::Background);
  50. paint(context, PaintPhase::Border);
  51. paint(context, PaintPhase::Foreground);
  52. if (context.has_focus())
  53. paint(context, PaintPhase::FocusOutline);
  54. paint(context, PaintPhase::Overlay);
  55. }
  56. void InitialContainingBlockBox::paint(PaintContext& context, PaintPhase phase)
  57. {
  58. stacking_context()->paint(context, phase);
  59. }
  60. HitTestResult InitialContainingBlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  61. {
  62. return stacking_context()->hit_test(position, type);
  63. }
  64. void InitialContainingBlockBox::recompute_selection_states()
  65. {
  66. SelectionState state = SelectionState::None;
  67. auto selection = this->selection().normalized();
  68. for_each_in_inclusive_subtree([&](auto& layout_node) {
  69. if (!selection.is_valid()) {
  70. // Everything gets SelectionState::None.
  71. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  72. state = SelectionState::StartAndEnd;
  73. } else if (&layout_node == selection.start().layout_node) {
  74. state = SelectionState::Start;
  75. } else if (&layout_node == selection.end().layout_node) {
  76. state = SelectionState::End;
  77. } else {
  78. if (state == SelectionState::Start)
  79. state = SelectionState::Full;
  80. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  81. state = SelectionState::None;
  82. }
  83. layout_node.set_selection_state(state);
  84. return IterationDecision::Continue;
  85. });
  86. }
  87. void InitialContainingBlockBox::set_selection(const LayoutRange& selection)
  88. {
  89. m_selection = selection;
  90. recompute_selection_states();
  91. }
  92. void InitialContainingBlockBox::set_selection_end(const LayoutPosition& position)
  93. {
  94. m_selection.set_end(position);
  95. recompute_selection_states();
  96. }
  97. }