2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2022-03-09 23:53:41 +01:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/Dump.h>
|
2021-09-08 11:27:46 +02:00
|
|
|
#include <LibWeb/Layout/InitialContainingBlock.h>
|
2022-03-10 23:13:37 +01:00
|
|
|
#include <LibWeb/Painting/PaintableBox.h>
|
2020-06-18 21:39:27 +02:00
|
|
|
#include <LibWeb/Painting/StackingContext.h>
|
2019-06-15 22:49:44 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
namespace Web::Layout {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2021-09-08 11:27:46 +02:00
|
|
|
InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
|
2021-10-06 20:02:41 +02:00
|
|
|
: BlockContainer(document, &document, move(style))
|
2019-06-15 22:49:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
InitialContainingBlock::~InitialContainingBlock() = default;
|
2019-06-16 21:35:03 +02:00
|
|
|
|
2022-03-21 10:56:02 +01:00
|
|
|
void InitialContainingBlock::build_stacking_context_tree_if_needed()
|
|
|
|
{
|
|
|
|
if (paint_box()->stacking_context())
|
|
|
|
return;
|
|
|
|
build_stacking_context_tree();
|
|
|
|
}
|
|
|
|
|
2021-09-08 11:27:46 +02:00
|
|
|
void InitialContainingBlock::build_stacking_context_tree()
|
2020-06-15 17:29:35 +02:00
|
|
|
{
|
2022-03-10 15:50:16 +01:00
|
|
|
const_cast<Painting::PaintableWithLines*>(paint_box())->set_stacking_context(make<Painting::StackingContext>(*this, nullptr));
|
2020-06-15 17:29:35 +02:00
|
|
|
|
2022-03-21 10:56:02 +01:00
|
|
|
for_each_in_subtree_of_type<Box>([&](Box& box) {
|
2022-04-07 15:29:21 +02:00
|
|
|
if (!box.paint_box())
|
|
|
|
return IterationDecision::Continue;
|
2022-03-21 10:56:02 +01:00
|
|
|
const_cast<Painting::PaintableBox*>(box.paint_box())->invalidate_stacking_context();
|
2020-06-15 17:29:35 +02:00
|
|
|
if (!box.establishes_stacking_context()) {
|
2022-03-10 15:50:16 +01:00
|
|
|
VERIFY(!box.paint_box()->stacking_context());
|
2020-06-15 17:29:35 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
}
|
2022-03-10 15:50:57 +01:00
|
|
|
auto* parent_context = const_cast<Painting::PaintableBox*>(box.paint_box())->enclosing_stacking_context();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(parent_context);
|
2022-03-10 15:50:57 +01:00
|
|
|
const_cast<Painting::PaintableBox*>(box.paint_box())->set_stacking_context(make<Painting::StackingContext>(box, parent_context));
|
2020-06-15 17:29:35 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2022-03-13 16:19:54 +01:00
|
|
|
|
|
|
|
const_cast<Painting::PaintableWithLines*>(paint_box())->stacking_context()->sort();
|
2020-06-15 17:29:35 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 11:27:46 +02:00
|
|
|
void InitialContainingBlock::paint_all_phases(PaintContext& context)
|
2020-06-15 17:29:35 +02:00
|
|
|
{
|
2022-03-21 10:56:02 +01:00
|
|
|
build_stacking_context_tree_if_needed();
|
2022-10-31 19:46:55 +00:00
|
|
|
context.painter().fill_rect(context.enclosing_device_rect(paint_box()->absolute_rect()).to_type<int>(), document().background_color(context.palette()));
|
2022-10-27 14:37:05 +01:00
|
|
|
context.painter().translate(-context.device_viewport_rect().location().to_type<int>());
|
2022-03-10 15:50:16 +01:00
|
|
|
paint_box()->stacking_context()->paint(context);
|
2020-06-15 17:29:35 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 11:27:46 +02:00
|
|
|
void InitialContainingBlock::recompute_selection_states()
|
2020-08-21 17:50:41 +02:00
|
|
|
{
|
|
|
|
SelectionState state = SelectionState::None;
|
|
|
|
|
|
|
|
auto selection = this->selection().normalized();
|
|
|
|
|
2021-04-06 18:38:10 +01:00
|
|
|
for_each_in_inclusive_subtree([&](auto& layout_node) {
|
2020-08-21 17:50:41 +02:00
|
|
|
if (!selection.is_valid()) {
|
|
|
|
// Everything gets SelectionState::None.
|
|
|
|
} else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
|
|
|
|
state = SelectionState::StartAndEnd;
|
|
|
|
} else if (&layout_node == selection.start().layout_node) {
|
|
|
|
state = SelectionState::Start;
|
|
|
|
} else if (&layout_node == selection.end().layout_node) {
|
|
|
|
state = SelectionState::End;
|
|
|
|
} else {
|
|
|
|
if (state == SelectionState::Start)
|
|
|
|
state = SelectionState::Full;
|
|
|
|
else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
|
|
|
|
state = SelectionState::None;
|
|
|
|
}
|
|
|
|
layout_node.set_selection_state(state);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void InitialContainingBlock::set_selection(LayoutRange const& selection)
|
2020-08-21 17:54:44 +02:00
|
|
|
{
|
|
|
|
m_selection = selection;
|
|
|
|
recompute_selection_states();
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
void InitialContainingBlock::set_selection_end(LayoutPosition const& position)
|
2020-08-21 17:54:44 +02:00
|
|
|
{
|
|
|
|
m_selection.set_end(position);
|
|
|
|
recompute_selection_states();
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|