| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-03-09 23:53:41 +01:00
										 |  |  |  * Copyright (c) 2018-2022, 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>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 11:26:01 +01:00
										 |  |  | #include <LibWeb/Painting/Paintable.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
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2022-03-10 11:12:06 +01:00
										 |  |  |     for (auto& line_box : paint_box()->line_boxes()) { | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |         for (auto& fragment : line_box.fragments()) { | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:16 +01:00
										 |  |  |             if (is<Box>(fragment.layout_node()) && verify_cast<Box>(fragment.layout_node()).paint_box()->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; | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:16 +01:00
										 |  |  |     return { paint_box()->absolute_border_box_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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 11:26:01 +01:00
										 |  |  | Painting::PaintableWithLines const* BlockContainer::paint_box() const | 
					
						
							| 
									
										
										
										
											2022-03-10 11:12:06 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-10 11:26:01 +01:00
										 |  |  |     return static_cast<Painting::PaintableWithLines const*>(Box::paint_box()); | 
					
						
							| 
									
										
										
										
											2022-03-10 11:12:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 14:02:25 +01:00
										 |  |  | OwnPtr<Painting::Paintable> BlockContainer::create_paintable() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Painting::PaintableWithLines::create(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |