LayoutBox.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <LibGfx/FloatRect.h>
  28. #include <LibWeb/Layout/LayoutNode.h>
  29. namespace Web {
  30. class LayoutBox : public LayoutNodeWithStyleAndBoxModelMetrics {
  31. public:
  32. const Gfx::FloatRect absolute_rect() const;
  33. Gfx::FloatPoint effective_offset() const;
  34. void set_offset(const Gfx::FloatPoint& offset);
  35. void set_offset(float x, float y) { set_offset({ x, y }); }
  36. const Gfx::FloatSize& size() const { return m_size; }
  37. void set_size(const Gfx::FloatSize&);
  38. void set_size(float width, float height) { set_size({ width, height }); }
  39. void set_width(float width) { set_size(width, height()); }
  40. void set_height(float height) { set_size(width(), height); }
  41. float width() const { return m_size.width(); }
  42. float height() const { return m_size.height(); }
  43. float absolute_x() const { return absolute_rect().x(); }
  44. float absolute_y() const { return absolute_rect().y(); }
  45. Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
  46. virtual HitTestResult hit_test(const Gfx::IntPoint& absolute_position) const override;
  47. virtual void set_needs_display() override;
  48. bool is_body() const;
  49. void set_containing_line_box_fragment(LineBoxFragment&);
  50. protected:
  51. LayoutBox(const Node* node, NonnullRefPtr<StyleProperties> style)
  52. : LayoutNodeWithStyleAndBoxModelMetrics(node, move(style))
  53. {
  54. }
  55. virtual void render(RenderingContext&) override;
  56. virtual void did_set_rect() { }
  57. private:
  58. virtual bool is_box() const override { return true; }
  59. enum class Edge {
  60. Top,
  61. Right,
  62. Bottom,
  63. Left,
  64. };
  65. void paint_border(RenderingContext&, Edge, const Gfx::FloatRect&, CSS::PropertyID style_property_id, CSS::PropertyID color_property_id, CSS::PropertyID width_property_id);
  66. Gfx::FloatPoint m_offset;
  67. Gfx::FloatSize m_size;
  68. // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
  69. WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
  70. };
  71. template<>
  72. inline bool is<LayoutBox>(const LayoutNode& node)
  73. {
  74. return node.is_box();
  75. }
  76. }