2020-01-18 11: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.
|
|
|
|
*/
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2020-07-26 18:16:18 +03:00
|
|
|
#include <AK/TypeCasts.h>
|
2019-06-15 23:49:44 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 14:04:00 +03:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2020-06-09 21:42:11 +03:00
|
|
|
#include <LibWeb/Forward.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/Layout/BoxModelMetrics.h>
|
|
|
|
#include <LibWeb/Layout/LayoutPosition.h>
|
2020-06-24 15:17:05 +03:00
|
|
|
#include <LibWeb/Layout/LayoutStyle.h>
|
2020-06-18 22:35:44 +03:00
|
|
|
#include <LibWeb/Painting/PaintContext.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/TreeNode.h>
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
namespace Web {
|
|
|
|
|
2019-09-29 00:02:22 +03:00
|
|
|
struct HitTestResult {
|
|
|
|
RefPtr<LayoutNode> layout_node;
|
2019-11-06 00:13:26 +03:00
|
|
|
int index_in_node { 0 };
|
2020-08-21 18:50:41 +03:00
|
|
|
|
|
|
|
enum InternalPosition {
|
|
|
|
None,
|
|
|
|
Before,
|
|
|
|
Inside,
|
|
|
|
After,
|
|
|
|
};
|
|
|
|
InternalPosition internal_position { None };
|
2019-09-29 00:02:22 +03:00
|
|
|
};
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-08-05 17:55:56 +03:00
|
|
|
enum class HitTestType {
|
|
|
|
Exact, // Exact matches only
|
|
|
|
TextCursor, // Clicking past the right/bottom edge of text will still hit the text
|
|
|
|
};
|
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
class LayoutNode : public TreeNode<LayoutNode> {
|
2019-06-15 23:49:44 +03:00
|
|
|
public:
|
|
|
|
virtual ~LayoutNode();
|
|
|
|
|
2020-08-05 17:55:56 +03:00
|
|
|
virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
|
2019-09-29 00:02:22 +03:00
|
|
|
|
2019-06-16 12:28:47 +03:00
|
|
|
bool is_anonymous() const { return !m_node; }
|
2020-07-26 20:37:56 +03:00
|
|
|
const DOM::Node* node() const { return m_node; }
|
2020-07-28 20:48:57 +03:00
|
|
|
DOM::Node* node() { return m_node; }
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-07-26 20:37:56 +03:00
|
|
|
DOM::Document& document() { return m_document; }
|
|
|
|
const DOM::Document& document() const { return m_document; }
|
2019-09-29 12:43:33 +03:00
|
|
|
|
2020-06-14 17:45:45 +03:00
|
|
|
const Frame& frame() const;
|
|
|
|
Frame& frame();
|
|
|
|
|
2019-11-04 21:37:52 +03:00
|
|
|
const LayoutDocument& root() const;
|
|
|
|
LayoutDocument& root();
|
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
virtual const char* class_name() const = 0;
|
|
|
|
virtual bool is_root() const { return false; }
|
2019-06-15 23:49:44 +03:00
|
|
|
virtual bool is_text() const { return false; }
|
2019-07-01 08:28:37 +03:00
|
|
|
virtual bool is_block() const { return false; }
|
2019-10-06 00:20:35 +03:00
|
|
|
virtual bool is_replaced() const { return false; }
|
2019-11-25 23:21:55 +03:00
|
|
|
virtual bool is_widget() const { return false; }
|
2020-06-06 00:36:02 +03:00
|
|
|
virtual bool is_frame() const { return false; }
|
2019-12-18 22:52:36 +03:00
|
|
|
virtual bool is_image() const { return false; }
|
2020-03-19 21:07:56 +03:00
|
|
|
virtual bool is_canvas() const { return false; }
|
2019-10-15 17:48:38 +03:00
|
|
|
virtual bool is_box() const { return false; }
|
2019-10-18 00:34:12 +03:00
|
|
|
virtual bool is_table() const { return false; }
|
|
|
|
virtual bool is_table_row() const { return false; }
|
|
|
|
virtual bool is_table_cell() const { return false; }
|
2020-06-09 22:08:15 +03:00
|
|
|
virtual bool is_table_row_group() const { return false; }
|
2020-07-26 18:16:18 +03:00
|
|
|
virtual bool is_break() const { return false; }
|
2020-09-11 19:17:39 +03:00
|
|
|
virtual bool is_check_box() const { return false; }
|
2020-09-12 18:56:11 +03:00
|
|
|
virtual bool is_button() const { return false; }
|
2019-10-15 15:19:52 +03:00
|
|
|
bool has_style() const { return m_has_style; }
|
2019-10-05 23:07:45 +03:00
|
|
|
|
|
|
|
bool is_inline() const { return m_inline; }
|
|
|
|
void set_inline(bool b) { m_inline = b; }
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-05-05 17:06:22 +03:00
|
|
|
bool is_inline_block() const { return is_inline() && is_block(); }
|
|
|
|
|
2020-09-11 19:15:47 +03:00
|
|
|
virtual bool wants_mouse_events() const { return false; }
|
|
|
|
|
|
|
|
virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
|
|
|
|
virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
|
|
|
|
virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
|
|
|
|
|
2020-05-26 22:53:10 +03:00
|
|
|
enum class LayoutMode {
|
|
|
|
Default,
|
|
|
|
AllPossibleLineBreaks,
|
|
|
|
OnlyRequiredLineBreaks,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void layout(LayoutMode);
|
2020-06-18 19:57:35 +03:00
|
|
|
|
|
|
|
enum class PaintPhase {
|
|
|
|
Background,
|
|
|
|
Border,
|
|
|
|
Foreground,
|
2020-08-14 20:40:37 +03:00
|
|
|
FocusOutline,
|
2020-06-18 19:57:35 +03:00
|
|
|
Overlay,
|
|
|
|
};
|
2020-10-06 02:11:33 +03:00
|
|
|
|
|
|
|
virtual void before_children_paint(PaintContext&, PaintPhase) {};
|
2020-06-18 22:35:44 +03:00
|
|
|
virtual void paint(PaintContext&, PaintPhase);
|
2020-10-06 02:11:33 +03:00
|
|
|
virtual void after_children_paint(PaintContext&, PaintPhase) {};
|
2019-06-16 22:35:03 +03:00
|
|
|
|
2020-06-26 16:08:42 +03:00
|
|
|
bool is_floating() const;
|
2020-06-05 17:54:28 +03:00
|
|
|
bool is_absolutely_positioned() const;
|
2020-06-12 15:19:03 +03:00
|
|
|
bool is_fixed_position() const;
|
2020-06-05 17:54:28 +03:00
|
|
|
|
2019-07-01 08:28:37 +03:00
|
|
|
const LayoutBlock* containing_block() const;
|
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
bool can_contain_boxes_with_position_absolute() const;
|
|
|
|
|
2019-07-08 18:42:23 +03:00
|
|
|
virtual LayoutNode& inline_wrapper() { return *this; }
|
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
const CSS::StyleProperties& specified_style() const;
|
2020-06-24 15:17:05 +03:00
|
|
|
const ImmutableLayoutStyle& style() const;
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2019-10-18 11:16:33 +03:00
|
|
|
LayoutNodeWithStyle* parent();
|
2019-10-07 10:50:31 +03:00
|
|
|
const LayoutNodeWithStyle* parent() const;
|
2019-07-24 08:34:07 +03:00
|
|
|
|
2020-05-26 22:53:10 +03:00
|
|
|
void inserted_into(LayoutNode&) { }
|
|
|
|
void removed_from(LayoutNode&) { }
|
|
|
|
void children_changed() { }
|
2019-09-29 18:40:39 +03:00
|
|
|
|
2020-05-26 22:53:10 +03:00
|
|
|
virtual void split_into_lines(LayoutBlock& container, LayoutMode);
|
2019-10-06 00:20:35 +03:00
|
|
|
|
2019-10-09 22:25:29 +03:00
|
|
|
bool is_visible() const { return m_visible; }
|
|
|
|
void set_visible(bool visible) { m_visible = visible; }
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
virtual void set_needs_display();
|
2019-10-09 22:25:29 +03:00
|
|
|
|
2019-10-18 00:32:08 +03:00
|
|
|
bool children_are_inline() const { return m_children_are_inline; }
|
|
|
|
void set_children_are_inline(bool value) { m_children_are_inline = value; }
|
|
|
|
|
2020-02-06 16:35:54 +03:00
|
|
|
Gfx::FloatPoint box_type_agnostic_position() const;
|
2019-10-20 10:14:12 +03:00
|
|
|
|
2020-06-07 18:55:46 +03:00
|
|
|
float font_size() const;
|
|
|
|
|
2020-08-21 18:50:41 +03:00
|
|
|
enum class SelectionState {
|
|
|
|
None, // No selection
|
|
|
|
Start, // Selection starts in this LayoutNode
|
|
|
|
End, // Selection ends in this LayoutNode
|
|
|
|
StartAndEnd, // Selection starts and ends in this LayoutNode
|
|
|
|
Full, // Selection starts before and ends after this LayoutNode
|
|
|
|
};
|
|
|
|
|
|
|
|
SelectionState selection_state() const { return m_selection_state; }
|
|
|
|
void set_selection_state(SelectionState state) { m_selection_state = state; }
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
protected:
|
2020-07-28 20:48:57 +03:00
|
|
|
LayoutNode(DOM::Document&, DOM::Node*);
|
2019-06-15 23:49:44 +03:00
|
|
|
|
|
|
|
private:
|
2019-10-07 10:50:31 +03:00
|
|
|
friend class LayoutNodeWithStyle;
|
|
|
|
|
2020-07-26 20:37:56 +03:00
|
|
|
DOM::Document& m_document;
|
2020-07-28 20:48:57 +03:00
|
|
|
DOM::Node* m_node { nullptr };
|
2019-06-25 20:46:01 +03:00
|
|
|
|
2019-10-05 23:07:45 +03:00
|
|
|
bool m_inline { false };
|
2019-10-07 10:50:31 +03:00
|
|
|
bool m_has_style { false };
|
2019-10-09 22:25:29 +03:00
|
|
|
bool m_visible { true };
|
2019-10-18 00:32:08 +03:00
|
|
|
bool m_children_are_inline { false };
|
2020-08-21 18:50:41 +03:00
|
|
|
SelectionState m_selection_state { SelectionState::None };
|
2019-06-15 23:49:44 +03:00
|
|
|
};
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
class LayoutNodeWithStyle : public LayoutNode {
|
|
|
|
public:
|
2020-05-26 22:53:10 +03:00
|
|
|
virtual ~LayoutNodeWithStyle() override { }
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
const CSS::StyleProperties& specified_style() const { return m_specified_style; }
|
|
|
|
void set_specified_style(const CSS::StyleProperties& style) { m_specified_style = style; }
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-06-24 15:17:05 +03:00
|
|
|
const ImmutableLayoutStyle& style() const { return static_cast<const ImmutableLayoutStyle&>(m_style); }
|
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
void apply_style(const CSS::StyleProperties&);
|
2020-06-24 20:41:12 +03:00
|
|
|
|
2019-10-07 10:50:31 +03:00
|
|
|
protected:
|
2020-07-28 20:48:57 +03:00
|
|
|
LayoutNodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
private:
|
2020-06-24 15:17:05 +03:00
|
|
|
LayoutStyle m_style;
|
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
NonnullRefPtr<CSS::StyleProperties> m_specified_style;
|
2020-06-24 00:15:23 +03:00
|
|
|
CSS::Position m_position;
|
2019-10-07 10:50:31 +03:00
|
|
|
};
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {
|
|
|
|
public:
|
|
|
|
BoxModelMetrics& box_model() { return m_box_model; }
|
|
|
|
const BoxModelMetrics& box_model() const { return m_box_model; }
|
|
|
|
|
|
|
|
protected:
|
2020-07-28 20:48:57 +03:00
|
|
|
LayoutNodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
2020-06-24 20:41:12 +03:00
|
|
|
: LayoutNodeWithStyle(document, node, move(style))
|
2019-10-15 17:48:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BoxModelMetrics m_box_model;
|
|
|
|
};
|
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
inline const CSS::StyleProperties& LayoutNode::specified_style() const
|
2019-10-07 10:50:31 +03:00
|
|
|
{
|
|
|
|
if (m_has_style)
|
2020-06-24 14:51:14 +03:00
|
|
|
return static_cast<const LayoutNodeWithStyle*>(this)->specified_style();
|
|
|
|
return parent()->specified_style();
|
2019-10-07 10:50:31 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 15:17:05 +03:00
|
|
|
inline const ImmutableLayoutStyle& LayoutNode::style() const
|
|
|
|
{
|
|
|
|
if (m_has_style)
|
|
|
|
return static_cast<const LayoutNodeWithStyle*>(this)->style();
|
|
|
|
return parent()->style();
|
|
|
|
}
|
|
|
|
|
2019-10-07 10:50:31 +03:00
|
|
|
inline const LayoutNodeWithStyle* LayoutNode::parent() const
|
|
|
|
{
|
|
|
|
return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
|
|
|
|
}
|
2019-10-15 15:19:52 +03:00
|
|
|
|
2019-10-18 11:16:33 +03:00
|
|
|
inline LayoutNodeWithStyle* LayoutNode::parent()
|
|
|
|
{
|
|
|
|
return static_cast<LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
|
|
|
|
}
|
|
|
|
|
2020-06-09 21:42:11 +03:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:16:18 +03:00
|
|
|
AK_BEGIN_TYPE_TRAITS(Web::LayoutNodeWithStyle)
|
|
|
|
static bool is_type(const Web::LayoutNode& node) { return node.has_style(); }
|
|
|
|
AK_END_TYPE_TRAITS()
|