Box.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #pragma once
  27. #include <AK/OwnPtr.h>
  28. #include <LibGfx/Rect.h>
  29. #include <LibWeb/Layout/LineBox.h>
  30. #include <LibWeb/Layout/Node.h>
  31. #include <LibWeb/Painting/StackingContext.h>
  32. namespace Web::Layout {
  33. class Box : public NodeWithStyleAndBoxModelMetrics {
  34. public:
  35. const Gfx::FloatRect absolute_rect() const;
  36. Gfx::FloatPoint effective_offset() const;
  37. void set_offset(const Gfx::FloatPoint& offset);
  38. void set_offset(float x, float y) { set_offset({ x, y }); }
  39. const Gfx::FloatSize& size() const { return m_size; }
  40. void set_size(const Gfx::FloatSize&);
  41. void set_size(float width, float height) { set_size({ width, height }); }
  42. void set_width(float width) { set_size(width, height()); }
  43. void set_height(float height) { set_size(width(), height); }
  44. float width() const { return m_size.width(); }
  45. float height() const { return m_size.height(); }
  46. float absolute_x() const { return absolute_rect().x(); }
  47. float absolute_y() const { return absolute_rect().y(); }
  48. Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
  49. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
  50. virtual void set_needs_display() override;
  51. bool is_body() const;
  52. void set_containing_line_box_fragment(LineBoxFragment&);
  53. bool establishes_stacking_context() const;
  54. StackingContext* stacking_context() { return m_stacking_context; }
  55. const StackingContext* stacking_context() const { return m_stacking_context; }
  56. void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
  57. StackingContext* enclosing_stacking_context();
  58. virtual void paint(PaintContext&, PaintPhase) override;
  59. Vector<LineBox>& line_boxes() { return m_line_boxes; }
  60. const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
  61. LineBox& ensure_last_line_box();
  62. LineBox& add_line_box();
  63. virtual float width_of_logical_containing_block() const;
  64. protected:
  65. Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  66. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  67. {
  68. }
  69. virtual void did_set_rect() { }
  70. Vector<LineBox> m_line_boxes;
  71. private:
  72. virtual bool is_box() const final { return true; }
  73. enum class Edge {
  74. Top,
  75. Right,
  76. Bottom,
  77. Left,
  78. };
  79. void paint_border(PaintContext&, Edge, const Gfx::FloatRect&, const BorderData&);
  80. Gfx::FloatPoint m_offset;
  81. Gfx::FloatSize m_size;
  82. // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
  83. WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
  84. OwnPtr<StackingContext> m_stacking_context;
  85. };
  86. }
  87. AK_BEGIN_TYPE_TRAITS(Web::Layout::Box)
  88. static bool is_type(const Web::Layout::Node& layout_node) { return layout_node.is_box(); }
  89. AK_END_TYPE_TRAITS()