mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-11-03 23:00:58 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			121 lines
		
	
	
	
		
			4.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			121 lines
		
	
	
	
		
			4.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * 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.
 | 
						|
 */
 | 
						|
 | 
						|
#include <LibGUI/Painter.h>
 | 
						|
#include <LibWeb/CSS/StyleResolver.h>
 | 
						|
#include <LibWeb/DOM/Element.h>
 | 
						|
#include <LibWeb/Dump.h>
 | 
						|
#include <LibWeb/Layout/BlockBox.h>
 | 
						|
#include <LibWeb/Layout/InitialContainingBlockBox.h>
 | 
						|
#include <LibWeb/Layout/InlineNode.h>
 | 
						|
#include <LibWeb/Layout/ReplacedBox.h>
 | 
						|
#include <LibWeb/Layout/TextNode.h>
 | 
						|
#include <LibWeb/Layout/WidgetBox.h>
 | 
						|
#include <math.h>
 | 
						|
 | 
						|
namespace Web::Layout {
 | 
						|
 | 
						|
BlockBox::BlockBox(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
 | 
						|
    : Box(document, node, move(style))
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
BlockBox::~BlockBox()
 | 
						|
{
 | 
						|
}
 | 
						|
 | 
						|
void BlockBox::paint(PaintContext& context, PaintPhase phase)
 | 
						|
{
 | 
						|
    if (!is_visible())
 | 
						|
        return;
 | 
						|
 | 
						|
    Box::paint(context, phase);
 | 
						|
 | 
						|
    if (!children_are_inline())
 | 
						|
        return;
 | 
						|
 | 
						|
    // FIXME: Inline backgrounds etc.
 | 
						|
    if (phase == PaintPhase::Foreground) {
 | 
						|
        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);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    if (phase == PaintPhase::FocusOutline) {
 | 
						|
        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());
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
HitTestResult BlockBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const
 | 
						|
{
 | 
						|
    if (!children_are_inline())
 | 
						|
        return Box::hit_test(position, type);
 | 
						|
 | 
						|
    HitTestResult last_good_candidate;
 | 
						|
    for (auto& line_box : m_line_boxes) {
 | 
						|
        for (auto& fragment : line_box.fragments()) {
 | 
						|
            if (is<Box>(fragment.layout_node()) && downcast<Box>(fragment.layout_node()).stacking_context())
 | 
						|
                continue;
 | 
						|
            if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
 | 
						|
                if (fragment.layout_node().is_block())
 | 
						|
                    return downcast<BlockBox>(fragment.layout_node()).hit_test(position, type);
 | 
						|
                return { fragment.layout_node(), fragment.text_index_at(position.x()) };
 | 
						|
            }
 | 
						|
            if (fragment.absolute_rect().top() <= position.y())
 | 
						|
                last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) };
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    if (type == HitTestType::TextCursor && last_good_candidate.layout_node)
 | 
						|
        return last_good_candidate;
 | 
						|
    return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
 | 
						|
}
 | 
						|
 | 
						|
void BlockBox::split_into_lines(BlockBox& container, LayoutMode layout_mode)
 | 
						|
{
 | 
						|
    auto* line_box = &container.ensure_last_line_box();
 | 
						|
    if (layout_mode != LayoutMode::OnlyRequiredLineBreaks && line_box->width() > 0 && line_box->width() + width() > container.width()) {
 | 
						|
        line_box = &container.add_line_box();
 | 
						|
    }
 | 
						|
    line_box->add_fragment(*this, 0, 0, width(), height());
 | 
						|
}
 | 
						|
 | 
						|
}
 |