| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  |  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 08:25:35 +01:00
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-12 13:27:28 +02:00
										 |  |  | #include <LibWeb/Dump.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | #include <LibWeb/Layout/BlockContainer.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | #include <LibWeb/Layout/InitialContainingBlock.h>
 | 
					
						
							| 
									
										
										
										
											2020-12-05 20:10:39 +01:00
										 |  |  | #include <LibWeb/Layout/InlineFormattingContext.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/ReplacedBox.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TextNode.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | namespace Web::Layout { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style) | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     : Box(document, node, move(style)) | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | BlockContainer::BlockContainer(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values) | 
					
						
							| 
									
										
										
										
											2021-01-06 14:10:53 +01:00
										 |  |  |     : Box(document, node, move(computed_values)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | BlockContainer::~BlockContainer() | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-20 23:00:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | bool BlockContainer::should_clip_overflow() const | 
					
						
							| 
									
										
										
										
											2021-02-22 19:19:11 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | void BlockContainer::paint(PaintContext& context, PaintPhase phase) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:40:37 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-09 21:25:29 +02:00
										 |  |  |     if (!is_visible()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     Box::paint(context, phase); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:40:37 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 17:17:11 +01:00
										 |  |  |     if (!children_are_inline()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 19:19:11 +01:00
										 |  |  |     if (should_clip_overflow()) { | 
					
						
							|  |  |  |         context.painter().save(); | 
					
						
							|  |  |  |         // FIXME: Handle overflow-x and overflow-y being different values.
 | 
					
						
							|  |  |  |         context.painter().add_clip_rect(enclosing_int_rect(padded_rect())); | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  |         context.painter().translate(-m_scroll_offset.to_type<int>()); | 
					
						
							| 
									
										
										
										
											2021-02-22 19:19:11 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 19:18:01 +01:00
										 |  |  |     for (auto& line_box : m_line_boxes) { | 
					
						
							|  |  |  |         for (auto& fragment : line_box.fragments()) { | 
					
						
							|  |  |  |             if (context.should_show_line_box_borders()) | 
					
						
							|  |  |  |                 context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green); | 
					
						
							|  |  |  |             fragment.paint(context, phase); | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-14 19:40:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 19:19:11 +01:00
										 |  |  |     if (should_clip_overflow()) { | 
					
						
							|  |  |  |         context.painter().restore(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-03 19:18:01 +01:00
										 |  |  |     // FIXME: Merge this loop with the above somehow..
 | 
					
						
							| 
									
										
										
										
											2020-08-14 19:40:37 +02:00
										 |  |  |     if (phase == PaintPhase::FocusOutline) { | 
					
						
							| 
									
										
										
										
											2020-12-03 17:17:11 +01:00
										 |  |  |         for (auto& line_box : m_line_boxes) { | 
					
						
							|  |  |  |             for (auto& fragment : line_box.fragments()) { | 
					
						
							|  |  |  |                 auto* node = fragment.layout_node().dom_node(); | 
					
						
							|  |  |  |                 if (!node) | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 auto* parent = node->parent_element(); | 
					
						
							|  |  |  |                 if (!parent) | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 if (parent->is_focused()) | 
					
						
							|  |  |  |                     context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline()); | 
					
						
							| 
									
										
										
										
											2020-08-14 19:40:37 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | HitTestResult BlockContainer::hit_test(const Gfx::IntPoint& position, HitTestType type) const | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!children_are_inline()) | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |         return Box::hit_test(position, type); | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-29 00:37:39 +02:00
										 |  |  |     HitTestResult last_good_candidate; | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |     for (auto& line_box : m_line_boxes) { | 
					
						
							|  |  |  |         for (auto& fragment : line_box.fragments()) { | 
					
						
							| 
									
										
										
										
											2021-06-24 19:53:42 +02:00
										 |  |  |             if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).stacking_context()) | 
					
						
							| 
									
										
										
										
											2020-07-01 19:02:28 +02:00
										 |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |             if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) { | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  |                 if (is<BlockContainer>(fragment.layout_node())) | 
					
						
							|  |  |  |                     return verify_cast<BlockContainer>(fragment.layout_node()).hit_test(position, type); | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  |                 return { fragment.layout_node(), fragment.text_index_at(position.x()) }; | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-06-29 00:37:39 +02:00
										 |  |  |             if (fragment.absolute_rect().top() <= position.y()) | 
					
						
							|  |  |  |                 last_good_candidate = { fragment.layout_node(), fragment.text_index_at(position.x()) }; | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-20 12:41:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 16:55:56 +02:00
										 |  |  |     if (type == HitTestType::TextCursor && last_good_candidate.layout_node) | 
					
						
							| 
									
										
										
										
											2020-06-29 00:37:39 +02:00
										 |  |  |         return last_good_candidate; | 
					
						
							| 
									
										
										
										
											2020-06-10 10:42:29 +02:00
										 |  |  |     return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr }; | 
					
						
							| 
									
										
										
										
											2019-09-25 12:40:37 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 23:47:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | bool BlockContainer::is_scrollable() const | 
					
						
							| 
									
										
										
										
											2021-02-22 23:44:51 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Support horizontal scroll as well (overflow-x)
 | 
					
						
							|  |  |  |     return computed_values().overflow_y() == CSS::Overflow::Scroll; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  | void BlockContainer::set_scroll_offset(const Gfx::FloatPoint& offset) | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-02 17:05:17 +01:00
										 |  |  |     // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
 | 
					
						
							|  |  |  |     if (offset.y() < 0 || m_scroll_offset == offset) | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     m_scroll_offset = offset; | 
					
						
							|  |  |  |     set_needs_display(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 23:22:28 +01:00
										 |  |  | bool BlockContainer::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned int, unsigned int, int wheel_delta_x, int wheel_delta_y) | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-22 23:44:51 +01:00
										 |  |  |     if (!is_scrollable()) | 
					
						
							| 
									
										
										
										
											2021-03-02 08:36:58 +11:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  |     auto new_offset = m_scroll_offset; | 
					
						
							| 
									
										
										
										
											2021-12-13 23:22:28 +01:00
										 |  |  |     new_offset.translate_by(wheel_delta_x, wheel_delta_y); | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  |     set_scroll_offset(new_offset); | 
					
						
							| 
									
										
										
										
											2021-03-02 08:36:58 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2021-02-22 19:48:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 21:53:25 +02:00
										 |  |  | LineBox& BlockContainer::ensure_last_line_box() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_line_boxes.is_empty()) | 
					
						
							|  |  |  |         return add_line_box(); | 
					
						
							|  |  |  |     return m_line_boxes.last(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LineBox& BlockContainer::add_line_box() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_line_boxes.append(LineBox()); | 
					
						
							|  |  |  |     return m_line_boxes.last(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |