LayoutNode.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include <AK/NonnullRefPtr.h>
  3. #include <AK/Vector.h>
  4. #include <LibDraw/Rect.h>
  5. #include <LibHTML/CSS/StyleProperties.h>
  6. #include <LibHTML/Layout/BoxModelMetrics.h>
  7. #include <LibHTML/RenderingContext.h>
  8. #include <LibHTML/TreeNode.h>
  9. class Document;
  10. class Element;
  11. class LayoutBlock;
  12. class LayoutNode;
  13. class LayoutNodeWithStyle;
  14. class LineBoxFragment;
  15. class Node;
  16. struct HitTestResult {
  17. RefPtr<LayoutNode> layout_node;
  18. };
  19. class LayoutNode : public TreeNode<LayoutNode> {
  20. public:
  21. virtual ~LayoutNode();
  22. const Rect& rect() const { return m_rect; }
  23. Rect& rect() { return m_rect; }
  24. void set_rect(const Rect& rect) { m_rect = rect; }
  25. BoxModelMetrics& box_model() { return m_box_metrics; }
  26. const BoxModelMetrics& box_model() const { return m_box_metrics; }
  27. virtual HitTestResult hit_test(const Point&) const;
  28. bool is_anonymous() const { return !m_node; }
  29. const Node* node() const { return m_node; }
  30. const Document& document() const;
  31. template<typename Callback>
  32. inline void for_each_child(Callback callback) const
  33. {
  34. for (auto* node = first_child(); node; node = node->next_sibling())
  35. callback(*node);
  36. }
  37. template<typename Callback>
  38. inline void for_each_child(Callback callback)
  39. {
  40. for (auto* node = first_child(); node; node = node->next_sibling())
  41. callback(*node);
  42. }
  43. virtual const char* class_name() const { return "LayoutNode"; }
  44. virtual bool is_text() const { return false; }
  45. virtual bool is_block() const { return false; }
  46. virtual bool is_replaced() const { return false; }
  47. bool is_inline() const { return m_inline; }
  48. void set_inline(bool b) { m_inline = b; }
  49. virtual void layout();
  50. virtual void render(RenderingContext&);
  51. const LayoutBlock* containing_block() const;
  52. virtual LayoutNode& inline_wrapper() { return *this; }
  53. const StyleProperties& style() const;
  54. const LayoutNodeWithStyle* parent() const;
  55. void inserted_into(LayoutNode&) {}
  56. void removed_from(LayoutNode&) {}
  57. virtual void split_into_lines(LayoutBlock& container);
  58. protected:
  59. explicit LayoutNode(const Node*);
  60. private:
  61. friend class LayoutNodeWithStyle;
  62. const Node* m_node { nullptr };
  63. BoxModelMetrics m_box_metrics;
  64. Rect m_rect;
  65. bool m_inline { false };
  66. bool m_has_style { false };
  67. };
  68. class LayoutNodeWithStyle : public LayoutNode {
  69. public:
  70. virtual ~LayoutNodeWithStyle() override {}
  71. const StyleProperties& style() const { return m_style; }
  72. protected:
  73. explicit LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StyleProperties> style)
  74. : LayoutNode(node)
  75. , m_style(move(style))
  76. {
  77. m_has_style = true;
  78. }
  79. private:
  80. NonnullRefPtr<StyleProperties> m_style;
  81. };
  82. inline const StyleProperties& LayoutNode::style() const
  83. {
  84. if (m_has_style)
  85. return static_cast<const LayoutNodeWithStyle*>(this)->style();
  86. return parent()->style();
  87. }
  88. inline const LayoutNodeWithStyle* LayoutNode::parent() const
  89. {
  90. return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  91. }