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
|
|
|
*/
|
|
|
|
|
2021-02-10 08:25:35 +01:00
|
|
|
#include <LibGfx/Painter.h>
|
2020-06-12 13:27:28 +02:00
|
|
|
#include <LibWeb/Dump.h>
|
2021-10-06 20:02:41 +02:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2021-09-08 11:27:46 +02:00
|
|
|
#include <LibWeb/Layout/InitialContainingBlock.h>
|
2020-12-05 20:10:39 +01:00
|
|
|
#include <LibWeb/Layout/InlineFormattingContext.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/ReplacedBox.h>
|
|
|
|
#include <LibWeb/Layout/TextNode.h>
|
2022-03-10 11:26:01 +01:00
|
|
|
#include <LibWeb/Painting/Paintable.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-10-06 20:02:41 +02:00
|
|
|
BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
2020-11-22 15:53:01 +01:00
|
|
|
: Box(document, node, move(style))
|
2019-06-15 22:49:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:02:41 +02:00
|
|
|
BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
|
2021-01-06 14:10:53 +01:00
|
|
|
: Box(document, node, move(computed_values))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:02:41 +02:00
|
|
|
BlockContainer::~BlockContainer()
|
2019-06-15 22:49:44 +02:00
|
|
|
{
|
|
|
|
}
|
2019-06-20 23:00:26 +02:00
|
|
|
|
2021-10-06 20:02:41 +02:00
|
|
|
HitTestResult BlockContainer::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
2019-10-03 15:20:13 +02:00
|
|
|
{
|
|
|
|
if (!children_are_inline())
|
2020-11-22 15:53:01 +01:00
|
|
|
return Box::hit_test(position, type);
|
2019-10-03 15:20:13 +02:00
|
|
|
|
2020-06-29 00:37:39 +02:00
|
|
|
HitTestResult last_good_candidate;
|
2022-03-10 11:12:06 +01:00
|
|
|
for (auto& line_box : paint_box()->line_boxes()) {
|
2019-10-03 15:20:13 +02:00
|
|
|
for (auto& fragment : line_box.fragments()) {
|
2022-03-10 15:50:16 +01:00
|
|
|
if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).paint_box()->stacking_context())
|
2020-07-01 19:02:28 +02:00
|
|
|
continue;
|
2020-06-10 10:42:29 +02:00
|
|
|
if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
|
2021-10-06 20:02:41 +02:00
|
|
|
if (is<BlockContainer>(fragment.layout_node()))
|
|
|
|
return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type);
|
2022-03-10 22:58:19 +01:00
|
|
|
return { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
|
2019-10-03 15:20:13 +02:00
|
|
|
}
|
2020-06-29 00:37:39 +02:00
|
|
|
if (fragment.absolute_rect().top() <= position.y())
|
2022-03-10 22:58:19 +01:00
|
|
|
last_good_candidate = { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
|
2019-10-03 15:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 12:41:31 +01:00
|
|
|
|
2022-03-10 22:58:19 +01:00
|
|
|
if (type == HitTestType::TextCursor && last_good_candidate.paintable)
|
2020-06-29 00:37:39 +02:00
|
|
|
return last_good_candidate;
|
2022-03-10 22:58:19 +01:00
|
|
|
return { paint_box()->absolute_border_box_rect().contains(position.x(), position.y()) ? paintable() : nullptr };
|
2019-09-25 12:40:37 +03:00
|
|
|
}
|
2019-10-05 23:47:06 +02:00
|
|
|
|
2021-10-06 20:02:41 +02:00
|
|
|
bool BlockContainer::is_scrollable() const
|
2021-02-22 23:44:51 +01:00
|
|
|
{
|
|
|
|
// FIXME: Support horizontal scroll as well (overflow-x)
|
|
|
|
return computed_values().overflow_y() == CSS::Overflow::Scroll;
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:02:41 +02:00
|
|
|
void BlockContainer::set_scroll_offset(const Gfx::FloatPoint& offset)
|
2021-02-22 19:48:24 +01:00
|
|
|
{
|
2021-03-02 17:05:17 +01:00
|
|
|
// FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
|
|
|
|
if (offset.y() < 0 || m_scroll_offset == offset)
|
2021-02-22 19:48:24 +01:00
|
|
|
return;
|
|
|
|
m_scroll_offset = offset;
|
|
|
|
set_needs_display();
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:26:01 +01:00
|
|
|
Painting::PaintableWithLines const* BlockContainer::paint_box() const
|
2022-03-10 11:12:06 +01:00
|
|
|
{
|
2022-03-10 11:26:01 +01:00
|
|
|
return static_cast<Painting::PaintableWithLines const*>(Box::paint_box());
|
2022-03-10 11:12:06 +01:00
|
|
|
}
|
|
|
|
|
2022-03-10 22:38:08 +01:00
|
|
|
RefPtr<Painting::Paintable> BlockContainer::create_paintable() const
|
2022-03-10 14:02:25 +01:00
|
|
|
{
|
|
|
|
return Painting::PaintableWithLines::create(*this);
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|