Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <LibWeb/Layout/LayoutBox.h>
30#include <LibWeb/Layout/LineBox.h>
31
32namespace Web {
33
34class Element;
35
36class LayoutBlock : public LayoutBox {
37public:
38 LayoutBlock(const Node*, NonnullRefPtr<StyleProperties>);
39 virtual ~LayoutBlock() override;
40
41 virtual const char* class_name() const override { return "LayoutBlock"; }
42
43 virtual void layout() override;
44 virtual void render(RenderingContext&) override;
45
46 virtual LayoutNode& inline_wrapper() override;
47
48 Vector<LineBox>& line_boxes() { return m_line_boxes; }
49 const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
50
51 LineBox& ensure_last_line_box();
52 LineBox& add_line_box();
53
54 virtual HitTestResult hit_test(const Gfx::Point&) const override;
55
56 LayoutBlock* previous_sibling() { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
57 const LayoutBlock* previous_sibling() const { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
58 LayoutBlock* next_sibling() { return to<LayoutBlock>(LayoutNode::next_sibling()); }
59 const LayoutBlock* next_sibling() const { return to<LayoutBlock>(LayoutNode::next_sibling()); }
60
61 template<typename Callback>
62 void for_each_fragment(Callback);
63 template<typename Callback>
64 void for_each_fragment(Callback) const;
65
66private:
67 virtual bool is_block() const override { return true; }
68
69 NonnullRefPtr<StyleProperties> style_for_anonymous_block() const;
70
71 void layout_inline_children();
72 void layout_block_children();
73
74 void compute_width();
75 void compute_position();
76 void compute_height();
77
78 Vector<LineBox> m_line_boxes;
79};
80
81template<typename Callback>
82void LayoutBlock::for_each_fragment(Callback callback)
83{
84 for (auto& line_box : line_boxes()) {
85 for (auto& fragment : line_box.fragments()) {
86 if (callback(fragment) == IterationDecision::Break)
87 return;
88 }
89 }
90}
91
92template<typename Callback>
93void LayoutBlock::for_each_fragment(Callback callback) const
94{
95 for (auto& line_box : line_boxes()) {
96 for (auto& fragment : line_box.fragments()) {
97 if (callback(fragment) == IterationDecision::Break)
98 return;
99 }
100 }
101}
102
103template<>
104inline bool is<LayoutBlock>(const LayoutNode& node)
105{
106 return node.is_block();
107}
108
109}