| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  | #include <AK/FileSystemPath.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | #include <LibCore/CFile.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  | #include <LibGUI/GApplication.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GScrollBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  | #include <LibGUI/GWindow.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:39:17 +01:00
										 |  |  | #include <LibGfx/PNGLoader.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | #include <LibHTML/DOM/Element.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  | #include <LibHTML/DOM/ElementFactory.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  | #include <LibHTML/DOM/HTMLAnchorElement.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  | #include <LibHTML/DOM/HTMLImageElement.h>
 | 
					
						
							|  |  |  | #include <LibHTML/DOM/Text.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <LibHTML/Dump.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-04 15:50:04 +02:00
										 |  |  | #include <LibHTML/Frame.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <LibHTML/HtmlView.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | #include <LibHTML/Layout/LayoutDocument.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <LibHTML/Layout/LayoutNode.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | #include <LibHTML/Parser/HTMLParser.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <LibHTML/RenderingContext.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-08 19:37:15 +02:00
										 |  |  | #include <LibHTML/ResourceLoader.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | HtmlView::HtmlView(GUI::Widget* parent) | 
					
						
							|  |  |  |     : GUI::ScrollableWidget(parent) | 
					
						
							|  |  |  |     , m_main_frame(::Frame::create(*this)) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-09 21:25:29 +02:00
										 |  |  |     main_frame().on_set_needs_display = [this](auto& content_rect) { | 
					
						
							| 
									
										
										
										
											2019-10-19 11:49:46 +02:00
										 |  |  |         if (content_rect.is_empty()) { | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  |         Gfx::Rect adjusted_rect = content_rect; | 
					
						
							| 
									
										
										
										
											2019-10-09 21:25:29 +02:00
										 |  |  |         adjusted_rect.set_location(to_widget_position(content_rect.location())); | 
					
						
							|  |  |  |         update(adjusted_rect); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     set_frame_shape(Gfx::FrameShape::Container); | 
					
						
							|  |  |  |     set_frame_shadow(Gfx::FrameShadow::Sunken); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     set_frame_thickness(2); | 
					
						
							|  |  |  |     set_should_hide_unnecessary_scrollbars(true); | 
					
						
							| 
									
										
										
										
											2020-01-05 00:09:35 +03:00
										 |  |  |     set_background_role(ColorRole::Base); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 15:50:04 +02:00
										 |  |  | HtmlView::~HtmlView() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | void HtmlView::set_document(Document* new_document) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     RefPtr<Document> old_document = document(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (new_document == old_document) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-10-06 22:58:18 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (old_document) | 
					
						
							|  |  |  |         old_document->on_layout_updated = nullptr; | 
					
						
							| 
									
										
										
										
											2019-10-06 22:58:18 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     main_frame().set_document(new_document); | 
					
						
							| 
									
										
										
										
											2019-10-07 19:33:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (new_document) { | 
					
						
							|  |  |  |         new_document->on_layout_updated = [this] { | 
					
						
							| 
									
										
										
										
											2019-10-07 19:33:06 +02:00
										 |  |  |             layout_and_sync_size(); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef HTML_DEBUG
 | 
					
						
							|  |  |  |     if (document != nullptr) { | 
					
						
							| 
									
										
										
										
											2019-10-03 10:25:00 +02:00
										 |  |  |         dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n"); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |         ::dump_tree(*layout_root()); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     layout_and_sync_size(); | 
					
						
							|  |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void HtmlView::layout_and_sync_size() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!document()) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     bool had_vertical_scrollbar = vertical_scrollbar().is_visible(); | 
					
						
							|  |  |  |     bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 15:50:04 +02:00
										 |  |  |     main_frame().set_size(available_size()); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     document()->layout(); | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |     set_content_size(enclosing_int_rect(layout_root()->rect()).size()); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
 | 
					
						
							|  |  |  |     //       since the scrollbars now take up some of the available space.
 | 
					
						
							|  |  |  |     if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) { | 
					
						
							|  |  |  |         main_frame().set_size(available_size()); | 
					
						
							|  |  |  |         document()->layout(); | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |         set_content_size(enclosing_int_rect(layout_root()->rect()).size()); | 
					
						
							| 
									
										
										
										
											2019-10-13 16:31:31 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  |     main_frame().set_viewport_rect(visible_content_rect()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #ifdef HTML_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-10-03 10:25:00 +02:00
										 |  |  |     dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n"); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     ::dump_tree(*layout_root()); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void HtmlView::resize_event(GUI::ResizeEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::ScrollableWidget::resize_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     layout_and_sync_size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void HtmlView::paint_event(GUI::PaintEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::Frame::paint_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     GUI::Painter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     painter.add_clip_rect(widget_inner_rect()); | 
					
						
							|  |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!layout_root()) { | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.fill_rect(event.rect(), palette().color(background_role())); | 
					
						
							| 
									
										
										
										
											2019-10-04 21:05:52 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-05 00:09:35 +03:00
										 |  |  |     painter.fill_rect(event.rect(), document()->background_color(palette())); | 
					
						
							| 
									
										
										
										
											2019-10-05 21:51:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (auto background_bitmap = document()->background_image()) { | 
					
						
							| 
									
										
										
										
											2019-10-19 11:49:46 +02:00
										 |  |  |         painter.draw_tiled_bitmap(event.rect(), *background_bitmap); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  |     painter.translate(frame_thickness(), frame_thickness()); | 
					
						
							|  |  |  |     painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |     RenderingContext context(painter, palette()); | 
					
						
							| 
									
										
										
										
											2019-10-12 15:02:53 +02:00
										 |  |  |     context.set_should_show_line_box_borders(m_should_show_line_box_borders); | 
					
						
							| 
									
										
										
										
											2019-10-15 21:52:01 +02:00
										 |  |  |     context.set_viewport_rect(visible_content_rect()); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     layout_root()->render(context); | 
					
						
							| 
									
										
										
										
											2019-09-25 12:44:22 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void HtmlView::mousemove_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!layout_root()) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         return GUI::ScrollableWidget::mousemove_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:50:35 +02:00
										 |  |  |     bool hovered_node_changed = false; | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  |     bool is_hovering_link = false; | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     bool was_hovering_link = document()->hovered_node() && document()->hovered_node()->is_link(); | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     auto result = layout_root()->hit_test(to_content_position(event.position())); | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |     const HTMLAnchorElement* hovered_link_element = nullptr; | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  |     if (result.layout_node) { | 
					
						
							| 
									
										
										
										
											2019-09-29 11:50:35 +02:00
										 |  |  |         auto* node = result.layout_node->node(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |         hovered_node_changed = node != document()->hovered_node(); | 
					
						
							|  |  |  |         document()->set_hovered_node(const_cast<Node*>(node)); | 
					
						
							| 
									
										
										
										
											2019-09-29 11:50:35 +02:00
										 |  |  |         if (node) { | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |             hovered_link_element = node->enclosing_link_element(); | 
					
						
							|  |  |  |             if (hovered_link_element) { | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  | #ifdef HTML_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |                 dbg() << "HtmlView: hovering over a link to " << hovered_link_element->href(); | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |                 is_hovering_link = true; | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  |         if (m_in_mouse_selection) { | 
					
						
							|  |  |  |             layout_root()->selection().set_end({ result.layout_node, result.index_in_node }); | 
					
						
							|  |  |  |             dump_selection("MouseMove"); | 
					
						
							|  |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  |     if (window()) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         window()->set_override_cursor(is_hovering_link ? GUI::StandardCursor::Hand : GUI::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  |     if (hovered_node_changed) { | 
					
						
							| 
									
										
										
										
											2019-09-29 11:50:35 +02:00
										 |  |  |         update(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |         auto* hovered_html_element = document()->hovered_node() ? document()->hovered_node()->enclosing_html_element() : nullptr; | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  |         if (hovered_html_element && !hovered_html_element->title().is_null()) { | 
					
						
							|  |  |  |             auto screen_position = screen_relative_rect().location().translated(event.position()); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             GUI::Application::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4)); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             GUI::Application::the().hide_tooltip(); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:24:36 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |     if (is_hovering_link != was_hovering_link) { | 
					
						
							|  |  |  |         if (on_link_hover) { | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |             on_link_hover(hovered_link_element ? document()->complete_url(hovered_link_element->href()).to_string() : String()); | 
					
						
							| 
									
										
										
										
											2019-10-19 21:21:29 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-28 23:02:22 +02:00
										 |  |  |     event.accept(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void HtmlView::mousedown_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     if (!layout_root()) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         return GUI::ScrollableWidget::mousemove_event(event); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool hovered_node_changed = false; | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  |     auto result = layout_root()->hit_test(to_content_position(event.position())); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  |     if (result.layout_node) { | 
					
						
							|  |  |  |         auto* node = result.layout_node->node(); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |         hovered_node_changed = node != document()->hovered_node(); | 
					
						
							|  |  |  |         document()->set_hovered_node(const_cast<Node*>(node)); | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  |         if (node) { | 
					
						
							|  |  |  |             if (auto* link = node->enclosing_link_element()) { | 
					
						
							|  |  |  |                 dbg() << "HtmlView: clicking on a link to " << link->href(); | 
					
						
							|  |  |  |                 if (on_link_click) | 
					
						
							|  |  |  |                     on_link_click(link->href()); | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |                 if (event.button() == GUI::MouseButton::Left) { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  |                     layout_root()->selection().set({ result.layout_node, result.index_in_node }, {}); | 
					
						
							|  |  |  |                     dump_selection("MouseDown"); | 
					
						
							|  |  |  |                     m_in_mouse_selection = true; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-09-29 12:04:02 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (hovered_node_changed) | 
					
						
							|  |  |  |         update(); | 
					
						
							|  |  |  |     event.accept(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void HtmlView::mouseup_event(GUI::MouseEvent& event) | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!layout_root()) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         return GUI::ScrollableWidget::mouseup_event(event); | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.button() == GUI::MouseButton::Left) { | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  |         dump_selection("MouseUp"); | 
					
						
							|  |  |  |         m_in_mouse_selection = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void HtmlView::keydown_event(GUI::KeyEvent& event) | 
					
						
							| 
									
										
										
										
											2019-10-17 21:36:22 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (event.modifiers() == 0) { | 
					
						
							|  |  |  |         switch (event.key()) { | 
					
						
							|  |  |  |         case Key_Home: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(0); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_End: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().max()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Down: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Up: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Left: | 
					
						
							|  |  |  |             horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_Right: | 
					
						
							|  |  |  |             horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_PageDown: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Key_PageUp: | 
					
						
							|  |  |  |             vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height()); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     event.accept(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | void HtmlView::reload() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     load(main_frame().document()->url()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  | static RefPtr<Document> create_image_document(const ByteBuffer& data, const URL& url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto document = adopt(*new Document); | 
					
						
							|  |  |  |     document->set_url(url); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     auto bitmap = Gfx::load_png_from_memory(data.data(), data.size()); | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  |     ASSERT(bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto html_element = create_element(document, "html"); | 
					
						
							|  |  |  |     document->append_child(html_element); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto head_element = create_element(document, "head"); | 
					
						
							|  |  |  |     html_element->append_child(head_element); | 
					
						
							|  |  |  |     auto title_element = create_element(document, "title"); | 
					
						
							|  |  |  |     head_element->append_child(title_element); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto basename = FileSystemPath(url.path()).basename(); | 
					
						
							|  |  |  |     auto title_text = adopt(*new Text(document, String::format("%s [%dx%d]", basename.characters(), bitmap->width(), bitmap->height()))); | 
					
						
							|  |  |  |     title_element->append_child(title_text); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto body_element = create_element(document, "body"); | 
					
						
							|  |  |  |     html_element->append_child(body_element); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto image_element = create_element(document, "img"); | 
					
						
							|  |  |  |     image_element->set_attribute("src", url.to_string()); | 
					
						
							|  |  |  |     body_element->append_child(image_element); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return document; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | void HtmlView::load(const URL& url) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     dbg() << "HtmlView::load: " << url; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  |     if (window()) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         window()->set_override_cursor(GUI::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2019-10-10 21:35:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  |     if (on_load_start) | 
					
						
							|  |  |  |         on_load_start(url); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-08 19:37:15 +02:00
										 |  |  |     ResourceLoader::the().load(url, [=](auto data) { | 
					
						
							|  |  |  |         if (data.is_null()) { | 
					
						
							|  |  |  |             dbg() << "Load failed!"; | 
					
						
							|  |  |  |             ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  |         RefPtr<Document> document; | 
					
						
							|  |  |  |         if (url.path().ends_with(".png")) { | 
					
						
							|  |  |  |             document = create_image_document(data, url); | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2019-11-06 20:52:18 +01:00
										 |  |  |             document = parse_html_document(data, url); | 
					
						
							| 
									
										
										
										
											2019-10-19 09:31:52 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         ASSERT(document); | 
					
						
							| 
									
										
										
										
											2019-10-08 19:37:15 +02:00
										 |  |  |         set_document(document); | 
					
						
							|  |  |  |         if (on_title_change) | 
					
						
							|  |  |  |             on_title_change(document->title()); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-10-05 10:16:27 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-13 12:34:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const LayoutDocument* HtmlView::layout_root() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return document() ? document()->layout_node() : nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LayoutDocument* HtmlView::layout_root() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!document()) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     return const_cast<LayoutDocument*>(document()->layout_node()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void HtmlView::scroll_to_anchor(const StringView& name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  |     if (!document()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 12:12:23 +02:00
										 |  |  |     auto* element = document()->get_element_by_id(name); | 
					
						
							| 
									
										
										
										
											2019-10-21 12:04:17 +02:00
										 |  |  |     if (!element) { | 
					
						
							|  |  |  |         auto candidates = document()->get_elements_by_name(name); | 
					
						
							|  |  |  |         for (auto* candidate : candidates) { | 
					
						
							|  |  |  |             if (is<HTMLAnchorElement>(*candidate)) { | 
					
						
							|  |  |  |                 element = to<HTMLAnchorElement>(candidate); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2019-10-21 12:01:30 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-21 12:04:17 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!element) { | 
					
						
							|  |  |  |         dbg() << "HtmlView::scroll_to_anchor(): Anchor not found: '" << name << "'"; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!element->layout_node()) { | 
					
						
							|  |  |  |         dbg() << "HtmlView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'"; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto& layout_node = *element->layout_node(); | 
					
						
							| 
									
										
										
										
											2019-11-18 16:25:38 +01:00
										 |  |  |     FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } }; | 
					
						
							|  |  |  |     scroll_into_view(enclosing_int_rect(float_rect), true, true); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     window()->set_override_cursor(GUI::StandardCursor::None); | 
					
						
							| 
									
										
										
										
											2019-10-20 09:14:12 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 10:36:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Document* HtmlView::document() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return main_frame().document(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Document* HtmlView::document() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return main_frame().document(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void HtmlView::dump_selection(const char* event_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     dbg() << event_name << " selection start: " | 
					
						
							|  |  |  |           << layout_root()->selection().start().layout_node << ":" << layout_root()->selection().start().index_in_node << ", end: " | 
					
						
							|  |  |  |           << layout_root()->selection().end().layout_node << ":" << layout_root()->selection().end().index_in_node; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-18 20:54:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void HtmlView::did_scroll() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     main_frame().set_viewport_rect(visible_content_rect()); | 
					
						
							|  |  |  | } |