summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Layout/Box.h
blob: 2248d81be3cd139a9d300297555f0a97d9706e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/OwnPtr.h>
#include <LibGfx/Rect.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/BorderPainting.h>
#include <LibWeb/Painting/StackingContext.h>

namespace Web::Layout {

class Box : public NodeWithStyleAndBoxModelMetrics {
public:
    const Gfx::FloatRect absolute_rect() const;

    Gfx::FloatPoint effective_offset() const;

    void set_offset(const Gfx::FloatPoint& offset);
    void set_offset(float x, float y) { set_offset({ x, y }); }

    const Gfx::FloatSize& size() const { return m_size; }
    void set_size(const Gfx::FloatSize&);
    void set_size(float width, float height) { set_size({ width, height }); }

    void set_width(float width) { set_size(width, height()); }
    void set_height(float height) { set_size(width(), height); }
    float width() const { return m_size.width(); }
    float height() const { return m_size.height(); }

    Gfx::FloatRect padded_rect() const
    {
        auto absolute_rect = this->absolute_rect();
        Gfx::FloatRect rect;
        rect.set_x(absolute_rect.x() - box_model().padding.left);
        rect.set_width(width() + box_model().padding.left + box_model().padding.right);
        rect.set_y(absolute_rect.y() - box_model().padding.top);
        rect.set_height(height() + box_model().padding.top + box_model().padding.bottom);
        return rect;
    }

    Gfx::FloatRect bordered_rect() const
    {
        auto padded_rect = this->padded_rect();
        Gfx::FloatRect rect;
        rect.set_x(padded_rect.x() - box_model().border.left);
        rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
        rect.set_y(padded_rect.y() - box_model().border.top);
        rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
        return rect;
    }

    float margin_box_width() const
    {
        auto margin_box = box_model().margin_box();
        return width() + margin_box.left + margin_box.right;
    }

    float margin_box_height() const
    {
        auto margin_box = box_model().margin_box();
        return height() + margin_box.top + margin_box.bottom;
    }

    float border_box_width() const
    {
        auto border_box = box_model().border_box();
        return width() + border_box.left + border_box.right;
    }

    float border_box_height() const
    {
        auto border_box = box_model().border_box();
        return height() + border_box.top + border_box.bottom;
    }

    Gfx::FloatRect content_box_as_relative_rect() const
    {
        return { m_offset, m_size };
    }

    Gfx::FloatRect margin_box_as_relative_rect() const
    {
        auto rect = content_box_as_relative_rect();
        auto margin_box = box_model().margin_box();
        rect.set_x(rect.x() - margin_box.left);
        rect.set_width(rect.width() + margin_box.left + margin_box.right);
        rect.set_y(rect.y() - margin_box.top);
        rect.set_height(rect.height() + margin_box.top + margin_box.bottom);
        return rect;
    }

    float absolute_x() const { return absolute_rect().x(); }
    float absolute_y() const { return absolute_rect().y(); }
    Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }

    bool is_out_of_flow(FormattingContext const&) const;

    virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
    virtual void set_needs_display() override;

    bool is_body() const;

    void set_containing_line_box_fragment(LineBoxFragment&);

    StackingContext* stacking_context() { return m_stacking_context; }
    const StackingContext* stacking_context() const { return m_stacking_context; }
    void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
    StackingContext* enclosing_stacking_context();

    virtual void paint(PaintContext&, PaintPhase) override;
    virtual void paint_border(PaintContext& context);
    virtual void paint_box_shadow(PaintContext& context);
    virtual void paint_background(PaintContext& context);

    virtual float width_of_logical_containing_block() const;

    Painting::BorderRadiusData normalized_border_radius_data();

protected:
    Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
        : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
    {
    }

    Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
        : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
    {
    }

    virtual void did_set_rect() { }

private:
    virtual bool is_box() const final { return true; }

    Gfx::FloatPoint m_offset;
    Gfx::FloatSize m_size;

    // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
    WeakPtr<LineBoxFragment> m_containing_line_box_fragment;

    OwnPtr<StackingContext> m_stacking_context;
};

template<>
inline bool Node::fast_is<Box>() const { return is_box(); }

}