2020-01-18 09:38:21 +01: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 20:33:02 +01:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/CSS/StyleResolver.h>
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
2020-06-12 13:27:28 +02:00
|
|
|
#include <LibWeb/Dump.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/Layout/LayoutBlock.h>
|
2020-06-12 14:19:03 +02:00
|
|
|
#include <LibWeb/Layout/LayoutDocument.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/Layout/LayoutInline.h>
|
|
|
|
#include <LibWeb/Layout/LayoutReplaced.h>
|
|
|
|
#include <LibWeb/Layout/LayoutText.h>
|
2020-06-05 19:13:27 +02:00
|
|
|
#include <LibWeb/Layout/LayoutWidget.h>
|
2019-10-20 12:30:25 +02:00
|
|
|
#include <math.h>
|
2019-06-15 22:49:44 +02:00
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
namespace Web {
|
|
|
|
|
2020-07-28 19:48:57 +02:00
|
|
|
LayoutBlock::LayoutBlock(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
2020-06-24 19:41:12 +02:00
|
|
|
: LayoutBox(document, node, move(style))
|
2019-06-15 22:49:44 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LayoutBlock::~LayoutBlock()
|
|
|
|
{
|
|
|
|
}
|
2019-06-20 23:00:26 +02:00
|
|
|
|
2019-07-08 17:42:23 +02:00
|
|
|
LayoutNode& LayoutBlock::inline_wrapper()
|
|
|
|
{
|
2020-11-22 14:46:36 +01:00
|
|
|
if (!last_child() || !last_child()->is_block() || last_child()->dom_node() != nullptr) {
|
2020-06-24 19:41:12 +02:00
|
|
|
append_child(adopt(*new LayoutBlock(document(), nullptr, style_for_anonymous_block())));
|
2019-10-17 23:32:08 +02:00
|
|
|
last_child()->set_children_are_inline(true);
|
2019-07-08 17:42:23 +02:00
|
|
|
}
|
|
|
|
return *last_child();
|
|
|
|
}
|
|
|
|
|
2020-06-18 21:35:44 +02:00
|
|
|
void LayoutBlock::paint(PaintContext& context, PaintPhase phase)
|
2019-09-25 12:40:37 +03:00
|
|
|
{
|
2019-10-09 21:25:29 +02:00
|
|
|
if (!is_visible())
|
|
|
|
return;
|
|
|
|
|
2020-06-18 21:35:44 +02:00
|
|
|
LayoutBox::paint(context, phase);
|
2019-09-25 12:40:37 +03:00
|
|
|
|
2020-06-18 18:57:35 +02: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 23:07:44 +02:00
|
|
|
fragment.paint(context);
|
2020-06-18 18:57:35 +02:00
|
|
|
}
|
2019-10-03 15:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 19:40:37 +02: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 14:46:36 +01:00
|
|
|
auto* node = fragment.layout_node().dom_node();
|
2020-08-14 19:40:37 +02: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 15:20:13 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 16:55:56 +02:00
|
|
|
HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
2019-10-03 15:20:13 +02:00
|
|
|
{
|
|
|
|
if (!children_are_inline())
|
2020-08-05 16:55:56 +02:00
|
|
|
return LayoutBox::hit_test(position, type);
|
2019-10-03 15:20:13 +02:00
|
|
|
|
2020-06-29 00:37:39 +02:00
|
|
|
HitTestResult last_good_candidate;
|
2019-10-03 15:20:13 +02:00
|
|
|
for (auto& line_box : m_line_boxes) {
|
|
|
|
for (auto& fragment : line_box.fragments()) {
|
2020-07-26 17:16:18 +02:00
|
|
|
if (is<LayoutBox>(fragment.layout_node()) && downcast<LayoutBox>(fragment.layout_node()).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)) {
|
2020-05-23 21:06:24 +02:00
|
|
|
if (fragment.layout_node().is_block())
|
2020-08-05 16:55:56 +02:00
|
|
|
return downcast<LayoutBlock>(fragment.layout_node()).hit_test(position, type);
|
2019-11-05 22:13:26 +01:00
|
|
|
return { fragment.layout_node(), 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())
|
|
|
|
last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
|
2019-10-03 15:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 12:41:31 +01:00
|
|
|
|
2020-08-05 16:55:56 +02:00
|
|
|
if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
|
2020-06-29 00:37:39 +02:00
|
|
|
return last_good_candidate;
|
2020-06-10 10:42:29 +02:00
|
|
|
return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
|
2019-09-25 12:40:37 +03:00
|
|
|
}
|
2019-10-05 23:47:06 +02:00
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
NonnullRefPtr<CSS::StyleProperties> LayoutBlock::style_for_anonymous_block() const
|
2019-10-05 23:47:06 +02:00
|
|
|
{
|
2020-07-26 20:01:35 +02:00
|
|
|
auto new_style = CSS::StyleProperties::create();
|
2019-10-05 23:47:06 +02:00
|
|
|
|
2020-06-24 13:51:14 +02:00
|
|
|
specified_style().for_each_property([&](auto property_id, auto& value) {
|
2020-07-26 20:01:35 +02:00
|
|
|
if (CSS::StyleResolver::is_inherited_property(property_id))
|
2019-10-08 15:34:19 +02:00
|
|
|
new_style->set_property(property_id, value);
|
2019-10-05 23:47:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return new_style;
|
|
|
|
}
|
2019-10-13 17:24:00 +02:00
|
|
|
|
2020-05-27 19:20:49 +02:00
|
|
|
void LayoutBlock::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
|
2020-05-05 16:06:22 +02:00
|
|
|
{
|
|
|
|
auto* line_box = &container.ensure_last_line_box();
|
2020-06-18 21:16:29 +02:00
|
|
|
if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + width() > container.width()) {
|
2020-05-05 16:06:22 +02:00
|
|
|
line_box = &container.add_line_box();
|
2020-06-18 21:16:29 +02:00
|
|
|
}
|
2020-05-05 16:06:22 +02:00
|
|
|
line_box->add_fragment(*this, 0, 0, width(), height());
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|