2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/CSS/StyleResolver.h>
|
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
2020-06-12 08:27:28 -03:00
|
|
|
#include <LibWeb/Dump.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/Layout/LayoutBlock.h>
|
2020-06-12 09:19:03 -03:00
|
|
|
#include <LibWeb/Layout/LayoutDocument.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/Layout/LayoutInline.h>
|
|
|
|
|
#include <LibWeb/Layout/LayoutReplaced.h>
|
|
|
|
|
#include <LibWeb/Layout/LayoutText.h>
|
2020-06-05 14:13:27 -03:00
|
|
|
#include <LibWeb/Layout/LayoutWidget.h>
|
2019-10-20 07:30:25 -03:00
|
|
|
#include <math.h>
|
2019-06-15 17:49:44 -03:00
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
namespace Web {
|
|
|
|
|
|
2020-07-28 14:48:57 -03:00
|
|
|
LayoutBlock::LayoutBlock(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
2020-06-24 14:41:12 -03:00
|
|
|
: LayoutBox(document, node, move(style))
|
2019-06-15 17:49:44 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LayoutBlock::~LayoutBlock()
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-06-20 18:00:26 -03:00
|
|
|
|
2019-07-08 12:42:23 -03:00
|
|
|
LayoutNode& LayoutBlock::inline_wrapper()
|
|
|
|
|
{
|
2020-11-22 10:46:36 -03:00
|
|
|
if (!last_child() || !last_child()->is_block() || last_child()->dom_node() != nullptr) {
|
2020-06-24 14:41:12 -03:00
|
|
|
append_child(adopt(*new LayoutBlock(document(), nullptr, style_for_anonymous_block())));
|
2019-10-17 18:32:08 -03:00
|
|
|
last_child()->set_children_are_inline(true);
|
2019-07-08 12:42:23 -03:00
|
|
|
}
|
|
|
|
|
return *last_child();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 16:35:44 -03:00
|
|
|
void LayoutBlock::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-06-18 16:35:44 -03:00
|
|
|
LayoutBox::paint(context, phase);
|
2019-09-25 06:40:37 -03:00
|
|
|
|
2020-06-18 13:57:35 -03:00
|
|
|
// FIXME: Inline backgrounds etc.
|
|
|
|
|
if (phase == PaintPhase::Foreground) {
|
|
|
|
|
if (children_are_inline()) {
|
|
|
|
|
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);
|
2020-06-28 18:07:44 -03:00
|
|
|
fragment.paint(context);
|
2020-06-18 13:57:35 -03:00
|
|
|
}
|
2019-10-03 10:20:13 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-14 14:40:37 -03:00
|
|
|
|
|
|
|
|
if (phase == PaintPhase::FocusOutline) {
|
|
|
|
|
if (children_are_inline()) {
|
|
|
|
|
for (auto& line_box : m_line_boxes) {
|
|
|
|
|
for (auto& fragment : line_box.fragments()) {
|
2020-11-22 10:46:36 -03:00
|
|
|
auto* node = fragment.layout_node().dom_node();
|
2020-08-14 14:40:37 -03:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-03 10:20:13 -03:00
|
|
|
}
|
|
|
|
|
|
2020-08-05 11:55:56 -03:00
|
|
|
HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
2019-10-03 10:20:13 -03:00
|
|
|
{
|
|
|
|
|
if (!children_are_inline())
|
2020-08-05 11:55:56 -03:00
|
|
|
return LayoutBox::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()) {
|
2020-07-26 12:16:18 -03:00
|
|
|
if (is<LayoutBox>(fragment.layout_node()) && downcast<LayoutBox>(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)) {
|
2020-05-23 16:06:24 -03:00
|
|
|
if (fragment.layout_node().is_block())
|
2020-08-05 11:55:56 -03:00
|
|
|
return downcast<LayoutBlock>(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
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
NonnullRefPtr<CSS::StyleProperties> LayoutBlock::style_for_anonymous_block() const
|
2019-10-05 18:47:06 -03:00
|
|
|
{
|
2020-07-26 15:01:35 -03:00
|
|
|
auto new_style = CSS::StyleProperties::create();
|
2019-10-05 18:47:06 -03:00
|
|
|
|
2020-06-24 08:51:14 -03:00
|
|
|
specified_style().for_each_property([&](auto property_id, auto& value) {
|
2020-07-26 15:01:35 -03:00
|
|
|
if (CSS::StyleResolver::is_inherited_property(property_id))
|
2019-10-08 10:34:19 -03:00
|
|
|
new_style->set_property(property_id, value);
|
2019-10-05 18:47:06 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new_style;
|
|
|
|
|
}
|
2019-10-13 12:24:00 -03:00
|
|
|
|
2020-05-27 14:20:49 -03:00
|
|
|
void LayoutBlock::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
|
2020-05-05 11:06:22 -03:00
|
|
|
{
|
|
|
|
|
auto* line_box = &container.ensure_last_line_box();
|
2020-06-18 16:16:29 -03:00
|
|
|
if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + width() > container.width()) {
|
2020-05-05 11:06:22 -03:00
|
|
|
line_box = &container.add_line_box();
|
2020-06-18 16:16:29 -03:00
|
|
|
}
|
2020-05-05 11:06:22 -03:00
|
|
|
line_box->add_fragment(*this, 0, 0, width(), height());
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|