LayoutBlock.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_height();
  56. void layout_absolutely_positioned_descendants();
  57. private:
  58. virtual bool is_block() const override { return true; }
  59. void place_block_level_non_replaced_element_in_normal_flow(LayoutBlock&);
  60. void place_block_level_replaced_element_in_normal_flow(LayoutReplaced&);
  61. void layout_absolutely_positioned_descendant(LayoutBox&);
  62. NonnullRefPtr<StyleProperties> style_for_anonymous_block() const;
  63. void layout_inside(LayoutMode);
  64. void layout_inline_children(LayoutMode);
  65. void layout_contained_boxes(LayoutMode);
  66. Vector<LineBox> m_line_boxes;
  67. };
  68. template<typename Callback>
  69. void LayoutBlock::for_each_fragment(Callback callback)
  70. {
  71. for (auto& line_box : line_boxes()) {
  72. for (auto& fragment : line_box.fragments()) {
  73. if (callback(fragment) == IterationDecision::Break)
  74. return;
  75. }
  76. }
  77. }
  78. template<typename Callback>
  79. void LayoutBlock::for_each_fragment(Callback callback) const
  80. {
  81. for (auto& line_box : line_boxes()) {
  82. for (auto& fragment : line_box.fragments()) {
  83. if (callback(fragment) == IterationDecision::Break)
  84. return;
  85. }
  86. }
  87. }
  88. template<>
  89. inline bool is<LayoutBlock>(const LayoutNode& node)
  90. {
  91. return node.is_block();
  92. }
  93. }