LayoutBlock.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <LibWeb/Layout/LayoutBox.h>
  28. #include <LibWeb/Layout/LineBox.h>
  29. namespace Web {
  30. class Element;
  31. class LayoutBlock : public LayoutBox {
  32. public:
  33. LayoutBlock(const Node*, NonnullRefPtr<StyleProperties>);
  34. virtual ~LayoutBlock() override;
  35. virtual const char* class_name() const override { return "LayoutBlock"; }
  36. virtual void layout(LayoutMode = LayoutMode::Default) override;
  37. virtual void render(RenderingContext&) override;
  38. virtual LayoutNode& inline_wrapper() override;
  39. Vector<LineBox>& line_boxes() { return m_line_boxes; }
  40. const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
  41. LineBox& ensure_last_line_box();
  42. LineBox& add_line_box();
  43. virtual HitTestResult hit_test(const Gfx::IntPoint&) const override;
  44. LayoutBlock* previous_sibling() { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
  45. const LayoutBlock* previous_sibling() const { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
  46. LayoutBlock* next_sibling() { return to<LayoutBlock>(LayoutNode::next_sibling()); }
  47. const LayoutBlock* next_sibling() const { return to<LayoutBlock>(LayoutNode::next_sibling()); }
  48. template<typename Callback>
  49. void for_each_fragment(Callback);
  50. template<typename Callback>
  51. void for_each_fragment(Callback) const;
  52. virtual void split_into_lines(LayoutBlock& container, LayoutMode) override;
  53. protected:
  54. void compute_width();
  55. void compute_position();
  56. void compute_height();
  57. private:
  58. virtual bool is_block() const override { return true; }
  59. NonnullRefPtr<StyleProperties> style_for_anonymous_block() const;
  60. void layout_children(LayoutMode);
  61. void layout_inline_children(LayoutMode);
  62. void layout_block_children(LayoutMode);
  63. Vector<LineBox> m_line_boxes;
  64. };
  65. template<typename Callback>
  66. void LayoutBlock::for_each_fragment(Callback callback)
  67. {
  68. for (auto& line_box : line_boxes()) {
  69. for (auto& fragment : line_box.fragments()) {
  70. if (callback(fragment) == IterationDecision::Break)
  71. return;
  72. }
  73. }
  74. }
  75. template<typename Callback>
  76. void LayoutBlock::for_each_fragment(Callback callback) const
  77. {
  78. for (auto& line_box : line_boxes()) {
  79. for (auto& fragment : line_box.fragments()) {
  80. if (callback(fragment) == IterationDecision::Break)
  81. return;
  82. }
  83. }
  84. }
  85. template<>
  86. inline bool is<LayoutBlock>(const LayoutNode& node)
  87. {
  88. return node.is_block();
  89. }
  90. }