2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-10-06 15:02:41 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-02-10 04:25:35 -03:00
|
|
|
#include <LibGfx/Painter.h>
|
2020-06-12 08:27:28 -03:00
|
|
|
#include <LibWeb/Dump.h>
|
2021-10-06 15:02:41 -03:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2021-09-08 06:27:46 -03:00
|
|
|
#include <LibWeb/Layout/InitialContainingBlock.h>
|
2020-12-05 16:10:39 -03:00
|
|
|
#include <LibWeb/Layout/InlineFormattingContext.h>
|
2020-11-22 11:53:01 -03:00
|
|
|
#include <LibWeb/Layout/ReplacedBox.h>
|
|
|
|
|
#include <LibWeb/Layout/TextNode.h>
|
2019-06-15 17:49:44 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
namespace Web::Layout {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
2020-11-22 11:53:01 -03:00
|
|
|
: Box(document, node, move(style))
|
2019-06-15 17:49:44 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
|
2021-01-06 10:10:53 -03:00
|
|
|
: Box(document, node, move(computed_values))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
BlockContainer::~BlockContainer()
|
2019-06-15 17:49:44 -03:00
|
|
|
{
|
|
|
|
|
}
|
2019-06-20 18:00:26 -03:00
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
bool BlockContainer::should_clip_overflow() const
|
2021-02-22 15:19:11 -03:00
|
|
|
{
|
|
|
|
|
return computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
void BlockContainer::paint(PaintContext& context, PaintPhase phase)
|
2019-09-25 06:40:37 -03:00
|
|
|
{
|
2019-10-09 16:25:29 -03:00
|
|
|
if (!is_visible())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
Box::paint(context, phase);
|
2019-09-25 06:40:37 -03:00
|
|
|
|
2020-12-03 13:17:11 -03:00
|
|
|
if (!children_are_inline())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-22 15:19:11 -03:00
|
|
|
if (should_clip_overflow()) {
|
|
|
|
|
context.painter().save();
|
|
|
|
|
// FIXME: Handle overflow-x and overflow-y being different values.
|
|
|
|
|
context.painter().add_clip_rect(enclosing_int_rect(padded_rect()));
|
2021-02-22 15:48:24 -03:00
|
|
|
context.painter().translate(-m_scroll_offset.to_type<int>());
|
2021-02-22 15:19:11 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-03 15:18:01 -03:00
|
|
|
for (auto& line_box : m_line_boxes) {
|
|
|
|
|
for (auto& fragment : line_box.fragments()) {
|
|
|
|
|
if (context.should_show_line_box_borders())
|
|
|
|
|
context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
|
|
|
|
|
fragment.paint(context, phase);
|
2019-10-03 10:20:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-14 14:40:37 -03:00
|
|
|
|
2021-02-22 15:19:11 -03:00
|
|
|
if (should_clip_overflow()) {
|
|
|
|
|
context.painter().restore();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 15:18:01 -03:00
|
|
|
// FIXME: Merge this loop with the above somehow..
|
2020-08-14 14:40:37 -03:00
|
|
|
if (phase == PaintPhase::FocusOutline) {
|
2020-12-03 13:17:11 -03:00
|
|
|
for (auto& line_box : m_line_boxes) {
|
|
|
|
|
for (auto& fragment : line_box.fragments()) {
|
|
|
|
|
auto* node = fragment.layout_node().dom_node();
|
|
|
|
|
if (!node)
|
|
|
|
|
continue;
|
|
|
|
|
auto* parent = node->parent_element();
|
|
|
|
|
if (!parent)
|
|
|
|
|
continue;
|
|
|
|
|
if (parent->is_focused())
|
|
|
|
|
context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
|
2020-08-14 14:40:37 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-03 10:20:13 -03:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
HitTestResult BlockContainer::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
2019-10-03 10:20:13 -03:00
|
|
|
{
|
|
|
|
|
if (!children_are_inline())
|
2020-11-22 11:53:01 -03:00
|
|
|
return Box::hit_test(position, type);
|
2019-10-03 10:20:13 -03:00
|
|
|
|
2020-06-28 19:37:39 -03:00
|
|
|
HitTestResult last_good_candidate;
|
2019-10-03 10:20:13 -03:00
|
|
|
for (auto& line_box : m_line_boxes) {
|
|
|
|
|
for (auto& fragment : line_box.fragments()) {
|
2021-06-24 14:53:42 -03:00
|
|
|
if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).stacking_context())
|
2020-07-01 14:02:28 -03:00
|
|
|
continue;
|
2020-06-10 05:42:29 -03:00
|
|
|
if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
|
2021-10-06 15:02:41 -03:00
|
|
|
if (is<BlockContainer>(fragment.layout_node()))
|
|
|
|
|
return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type);
|
2019-11-05 18:13:26 -03:00
|
|
|
return { fragment.layout_node(), fragment.text_index_at(position.x()) };
|
2019-10-03 10:20:13 -03:00
|
|
|
}
|
2020-06-28 19:37:39 -03:00
|
|
|
if (fragment.absolute_rect().top() <= position.y())
|
|
|
|
|
last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
|
2019-10-03 10:20:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-20 08:41:31 -03:00
|
|
|
|
2020-08-05 11:55:56 -03:00
|
|
|
if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
|
2020-06-28 19:37:39 -03:00
|
|
|
return last_good_candidate;
|
2020-06-10 05:42:29 -03:00
|
|
|
return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
|
2019-09-25 06:40:37 -03:00
|
|
|
}
|
2019-10-05 18:47:06 -03:00
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
bool BlockContainer::is_scrollable() const
|
2021-02-22 19:44:51 -03:00
|
|
|
{
|
|
|
|
|
// FIXME: Support horizontal scroll as well (overflow-x)
|
|
|
|
|
return computed_values().overflow_y() == CSS::Overflow::Scroll;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 15:02:41 -03:00
|
|
|
void BlockContainer::set_scroll_offset(const Gfx::FloatPoint& offset)
|
2021-02-22 15:48:24 -03:00
|
|
|
{
|
2021-03-02 13:05:17 -03: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 15:48:24 -03:00
|
|
|
return;
|
|
|
|
|
m_scroll_offset = offset;
|
|
|
|
|
set_needs_display();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 19:22:28 -03:00
|
|
|
bool BlockContainer::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned int, unsigned int, int wheel_delta_x, int wheel_delta_y)
|
2021-02-22 15:48:24 -03:00
|
|
|
{
|
2021-02-22 19:44:51 -03:00
|
|
|
if (!is_scrollable())
|
2021-03-01 18:36:58 -03:00
|
|
|
return false;
|
2021-02-22 15:48:24 -03:00
|
|
|
auto new_offset = m_scroll_offset;
|
2021-12-13 19:22:28 -03:00
|
|
|
new_offset.translate_by(wheel_delta_x, wheel_delta_y);
|
2021-02-22 15:48:24 -03:00
|
|
|
set_scroll_offset(new_offset);
|
2021-03-01 18:36:58 -03:00
|
|
|
|
|
|
|
|
return true;
|
2021-02-22 15:48:24 -03:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 16:53:25 -03:00
|
|
|
LineBox& BlockContainer::ensure_last_line_box()
|
|
|
|
|
{
|
|
|
|
|
if (m_line_boxes.is_empty())
|
|
|
|
|
return add_line_box();
|
|
|
|
|
return m_line_boxes.last();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LineBox& BlockContainer::add_line_box()
|
|
|
|
|
{
|
|
|
|
|
m_line_boxes.append(LineBox());
|
|
|
|
|
return m_line_boxes.last();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|