Box.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <LibGUI/Painter.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/HTML/HTMLBodyElement.h>
  29. #include <LibWeb/Layout/BlockBox.h>
  30. #include <LibWeb/Layout/Box.h>
  31. #include <LibWeb/Page/Frame.h>
  32. #include <LibWeb/Painting/BorderPainting.h>
  33. namespace Web::Layout {
  34. void Box::paint(PaintContext& context, PaintPhase phase)
  35. {
  36. if (!is_visible())
  37. return;
  38. Gfx::PainterStateSaver saver(context.painter());
  39. if (is_fixed_position())
  40. context.painter().translate(context.scroll_offset());
  41. Gfx::FloatRect padded_rect;
  42. padded_rect.set_x(absolute_x() - box_model().padding.left);
  43. padded_rect.set_width(width() + box_model().padding.left + box_model().padding.right);
  44. padded_rect.set_y(absolute_y() - box_model().padding.top);
  45. padded_rect.set_height(height() + box_model().padding.top + box_model().padding.bottom);
  46. if (phase == PaintPhase::Background && !is_body()) {
  47. // FIXME: We should paint the body here too, but that currently happens at the view layer.
  48. auto bgcolor = specified_style().property(CSS::PropertyID::BackgroundColor);
  49. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  50. context.painter().fill_rect(enclosing_int_rect(padded_rect), bgcolor.value()->to_color(document()));
  51. }
  52. auto bgimage = specified_style().property(CSS::PropertyID::BackgroundImage);
  53. if (bgimage.has_value() && bgimage.value()->is_image()) {
  54. auto& image_value = static_cast<const CSS::ImageStyleValue&>(*bgimage.value());
  55. if (image_value.bitmap()) {
  56. context.painter().draw_tiled_bitmap(enclosing_int_rect(padded_rect), *image_value.bitmap());
  57. }
  58. }
  59. }
  60. if (phase == PaintPhase::Border) {
  61. Gfx::FloatRect bordered_rect;
  62. bordered_rect.set_x(padded_rect.x() - box_model().border.left);
  63. bordered_rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
  64. bordered_rect.set_y(padded_rect.y() - box_model().border.top);
  65. bordered_rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
  66. Painting::paint_border(context, Painting::BorderEdge::Left, bordered_rect, style());
  67. Painting::paint_border(context, Painting::BorderEdge::Right, bordered_rect, style());
  68. Painting::paint_border(context, Painting::BorderEdge::Top, bordered_rect, style());
  69. Painting::paint_border(context, Painting::BorderEdge::Bottom, bordered_rect, style());
  70. }
  71. Layout::NodeWithStyleAndBoxModelMetrics::paint(context, phase);
  72. if (phase == PaintPhase::Overlay && dom_node() && document().inspected_node() == dom_node()) {
  73. auto content_rect = absolute_rect();
  74. auto margin_box = box_model().margin_box();
  75. Gfx::FloatRect margin_rect;
  76. margin_rect.set_x(absolute_x() - margin_box.left);
  77. margin_rect.set_width(width() + margin_box.left + margin_box.right);
  78. margin_rect.set_y(absolute_y() - margin_box.top);
  79. margin_rect.set_height(height() + margin_box.top + margin_box.bottom);
  80. context.painter().draw_rect(enclosing_int_rect(margin_rect), Color::Yellow);
  81. context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan);
  82. context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
  83. }
  84. if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && downcast<DOM::Element>(*dom_node()).is_focused()) {
  85. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  86. }
  87. }
  88. HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  89. {
  90. // FIXME: It would be nice if we could confidently skip over hit testing
  91. // parts of the layout tree, but currently we can't just check
  92. // m_rect.contains() since inline text rects can't be trusted..
  93. HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  94. for_each_child_in_paint_order([&](auto& child) {
  95. auto child_result = child.hit_test(position, type);
  96. if (child_result.layout_node)
  97. result = child_result;
  98. });
  99. return result;
  100. }
  101. void Box::set_needs_display()
  102. {
  103. if (!is_inline()) {
  104. frame().set_needs_display(enclosing_int_rect(absolute_rect()));
  105. return;
  106. }
  107. Node::set_needs_display();
  108. }
  109. bool Box::is_body() const
  110. {
  111. return dom_node() && dom_node() == document().body();
  112. }
  113. void Box::set_offset(const Gfx::FloatPoint& offset)
  114. {
  115. if (m_offset == offset)
  116. return;
  117. m_offset = offset;
  118. did_set_rect();
  119. }
  120. void Box::set_size(const Gfx::FloatSize& size)
  121. {
  122. if (m_size == size)
  123. return;
  124. m_size = size;
  125. did_set_rect();
  126. }
  127. Gfx::FloatPoint Box::effective_offset() const
  128. {
  129. if (m_containing_line_box_fragment)
  130. return m_containing_line_box_fragment->offset();
  131. return m_offset;
  132. }
  133. const Gfx::FloatRect Box::absolute_rect() const
  134. {
  135. Gfx::FloatRect rect { effective_offset(), size() };
  136. for (auto* block = containing_block(); block; block = block->containing_block()) {
  137. rect.move_by(block->effective_offset());
  138. }
  139. return rect;
  140. }
  141. void Box::set_containing_line_box_fragment(LineBoxFragment& fragment)
  142. {
  143. m_containing_line_box_fragment = fragment.make_weak_ptr();
  144. }
  145. StackingContext* Box::enclosing_stacking_context()
  146. {
  147. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  148. if (!ancestor->is_box())
  149. continue;
  150. auto& ancestor_box = downcast<Box>(*ancestor);
  151. if (!ancestor_box.establishes_stacking_context())
  152. continue;
  153. ASSERT(ancestor_box.stacking_context());
  154. return ancestor_box.stacking_context();
  155. }
  156. // We should always reach the Layout::InitialContainingBlockBox stacking context.
  157. ASSERT_NOT_REACHED();
  158. }
  159. bool Box::establishes_stacking_context() const
  160. {
  161. if (!has_style())
  162. return false;
  163. if (dom_node() == document().root())
  164. return true;
  165. auto position = style().position();
  166. auto z_index = style().z_index();
  167. if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
  168. if (z_index.has_value())
  169. return true;
  170. }
  171. if (position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  172. return true;
  173. return false;
  174. }
  175. LineBox& Box::ensure_last_line_box()
  176. {
  177. if (m_line_boxes.is_empty())
  178. return add_line_box();
  179. return m_line_boxes.last();
  180. }
  181. LineBox& Box::add_line_box()
  182. {
  183. m_line_boxes.append(LineBox());
  184. return m_line_boxes.last();
  185. }
  186. float Box::width_of_logical_containing_block() const
  187. {
  188. auto* containing_block = this->containing_block();
  189. ASSERT(containing_block);
  190. return containing_block->width();
  191. }
  192. }