PageView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <AK/LexicalPath.h>
  27. #include <AK/URL.h>
  28. #include <LibCore/File.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/ScrollBar.h>
  33. #include <LibGUI/Window.h>
  34. #include <LibGfx/ImageDecoder.h>
  35. #include <LibJS/Runtime/Value.h>
  36. #include <LibWeb/DOM/Element.h>
  37. #include <LibWeb/DOM/ElementFactory.h>
  38. #include <LibWeb/DOM/HTMLAnchorElement.h>
  39. #include <LibWeb/DOM/HTMLImageElement.h>
  40. #include <LibWeb/DOM/MouseEvent.h>
  41. #include <LibWeb/DOM/Text.h>
  42. #include <LibWeb/Dump.h>
  43. #include <LibWeb/Frame/EventHandler.h>
  44. #include <LibWeb/Frame/Frame.h>
  45. #include <LibWeb/Layout/LayoutDocument.h>
  46. #include <LibWeb/Layout/LayoutNode.h>
  47. #include <LibWeb/Loader/ResourceLoader.h>
  48. #include <LibWeb/PageView.h>
  49. #include <LibWeb/Painting/PaintContext.h>
  50. #include <LibWeb/Parser/HTMLDocumentParser.h>
  51. #include <stdio.h>
  52. //#define SELECTION_DEBUG
  53. namespace Web {
  54. PageView::PageView()
  55. : m_page(make<Page>(*this))
  56. {
  57. set_should_hide_unnecessary_scrollbars(true);
  58. set_background_role(ColorRole::Base);
  59. }
  60. PageView::~PageView()
  61. {
  62. }
  63. void PageView::page_did_change_title(const String& title)
  64. {
  65. if (on_title_change)
  66. on_title_change(title);
  67. }
  68. void PageView::page_did_set_document_in_main_frame(Document* document)
  69. {
  70. if (on_set_document)
  71. on_set_document(document);
  72. layout_and_sync_size();
  73. scroll_to_top();
  74. update();
  75. }
  76. void PageView::page_did_start_loading(const URL& url)
  77. {
  78. if (on_load_start)
  79. on_load_start(url);
  80. }
  81. void PageView::page_did_change_selection()
  82. {
  83. update();
  84. }
  85. void PageView::page_did_request_cursor_change(GUI::StandardCursor cursor)
  86. {
  87. if (window())
  88. window()->set_override_cursor(cursor);
  89. }
  90. void PageView::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  91. {
  92. if (on_link_context_menu_request)
  93. on_link_context_menu_request(href, screen_relative_rect().location().translated(to_widget_position(content_position)));
  94. }
  95. void PageView::page_did_click_link(const String& href, const String& target, unsigned modifiers)
  96. {
  97. if (on_link_click)
  98. on_link_click(href, target, modifiers);
  99. }
  100. void PageView::page_did_middle_click_link(const String& href, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  101. {
  102. if (on_link_middle_click)
  103. on_link_middle_click(href);
  104. }
  105. void PageView::page_did_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title)
  106. {
  107. GUI::Application::the().show_tooltip(title, screen_relative_rect().location().translated(to_widget_position(content_position)));
  108. }
  109. void PageView::page_did_leave_tooltip_area()
  110. {
  111. GUI::Application::the().hide_tooltip();
  112. }
  113. void PageView::page_did_hover_link(const URL& url)
  114. {
  115. if (on_link_hover)
  116. on_link_hover(url.to_string());
  117. }
  118. void PageView::page_did_unhover_link()
  119. {
  120. }
  121. void PageView::page_did_request_scroll_to_anchor(const String& fragment)
  122. {
  123. scroll_to_anchor(fragment);
  124. }
  125. void PageView::page_did_invalidate(const Gfx::IntRect&)
  126. {
  127. update();
  128. }
  129. void PageView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
  130. {
  131. if (on_favicon_change)
  132. on_favicon_change(bitmap);
  133. }
  134. void PageView::layout_and_sync_size()
  135. {
  136. if (!document())
  137. return;
  138. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  139. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  140. page().main_frame().set_size(available_size());
  141. document()->layout();
  142. set_content_size(layout_root()->size().to_int_size());
  143. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  144. // since the scrollbars now take up some of the available space.
  145. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  146. page().main_frame().set_size(available_size());
  147. document()->layout();
  148. set_content_size(layout_root()->size().to_int_size());
  149. }
  150. page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  151. #ifdef HTML_DEBUG
  152. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  153. ::dump_tree(*layout_root());
  154. #endif
  155. }
  156. void PageView::resize_event(GUI::ResizeEvent& event)
  157. {
  158. GUI::ScrollableWidget::resize_event(event);
  159. layout_and_sync_size();
  160. }
  161. void PageView::paint_event(GUI::PaintEvent& event)
  162. {
  163. GUI::Frame::paint_event(event);
  164. GUI::Painter painter(*this);
  165. painter.add_clip_rect(widget_inner_rect());
  166. painter.add_clip_rect(event.rect());
  167. if (!layout_root()) {
  168. painter.fill_rect(event.rect(), palette().color(background_role()));
  169. return;
  170. }
  171. painter.fill_rect(event.rect(), document()->background_color(palette()));
  172. if (auto background_bitmap = document()->background_image()) {
  173. painter.draw_tiled_bitmap(event.rect(), *background_bitmap);
  174. }
  175. painter.translate(frame_thickness(), frame_thickness());
  176. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  177. PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
  178. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  179. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  180. layout_root()->paint_all_phases(context);
  181. }
  182. void PageView::mousemove_event(GUI::MouseEvent& event)
  183. {
  184. page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
  185. GUI::ScrollableWidget::mousemove_event(event);
  186. }
  187. void PageView::mousedown_event(GUI::MouseEvent& event)
  188. {
  189. page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
  190. GUI::ScrollableWidget::mousedown_event(event);
  191. }
  192. void PageView::mouseup_event(GUI::MouseEvent& event)
  193. {
  194. page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
  195. GUI::ScrollableWidget::mouseup_event(event);
  196. }
  197. void PageView::keydown_event(GUI::KeyEvent& event)
  198. {
  199. if (event.modifiers() == 0) {
  200. switch (event.key()) {
  201. case Key_Home:
  202. vertical_scrollbar().set_value(0);
  203. break;
  204. case Key_End:
  205. vertical_scrollbar().set_value(vertical_scrollbar().max());
  206. break;
  207. case Key_Down:
  208. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  209. break;
  210. case Key_Up:
  211. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  212. break;
  213. case Key_Left:
  214. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  215. break;
  216. case Key_Right:
  217. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  218. break;
  219. case Key_PageDown:
  220. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  221. break;
  222. case Key_PageUp:
  223. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. event.accept();
  230. }
  231. void PageView::reload()
  232. {
  233. load(page().main_frame().document()->url());
  234. }
  235. void PageView::load_html(const StringView& html, const URL& url)
  236. {
  237. HTMLDocumentParser parser(html, "utf-8");
  238. parser.run(url);
  239. set_document(&parser.document());
  240. }
  241. bool PageView::load(const URL& url)
  242. {
  243. if (window())
  244. window()->set_override_cursor(GUI::StandardCursor::None);
  245. return page().main_frame().loader().load(url);
  246. }
  247. const LayoutDocument* PageView::layout_root() const
  248. {
  249. return document() ? document()->layout_node() : nullptr;
  250. }
  251. LayoutDocument* PageView::layout_root()
  252. {
  253. if (!document())
  254. return nullptr;
  255. return const_cast<LayoutDocument*>(document()->layout_node());
  256. }
  257. void PageView::scroll_to_anchor(const StringView& name)
  258. {
  259. if (!document())
  260. return;
  261. const auto* element = document()->get_element_by_id(name);
  262. if (!element) {
  263. auto candidates = document()->get_elements_by_name(name);
  264. for (auto* candidate : candidates) {
  265. if (is<HTMLAnchorElement>(*candidate)) {
  266. element = to<HTMLAnchorElement>(candidate);
  267. break;
  268. }
  269. }
  270. }
  271. if (!element) {
  272. dbg() << "PageView::scroll_to_anchor(): Anchor not found: '" << name << "'";
  273. return;
  274. }
  275. if (!element->layout_node()) {
  276. dbg() << "PageView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'";
  277. return;
  278. }
  279. auto& layout_node = *element->layout_node();
  280. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } };
  281. scroll_into_view(enclosing_int_rect(float_rect), true, true);
  282. window()->set_override_cursor(GUI::StandardCursor::None);
  283. }
  284. void PageView::set_use_old_parser(bool use_old_parser)
  285. {
  286. page().main_frame().loader().set_use_old_parser(use_old_parser);
  287. }
  288. void PageView::load_empty_document()
  289. {
  290. page().main_frame().set_document(nullptr);
  291. }
  292. Document* PageView::document()
  293. {
  294. return page().main_frame().document();
  295. }
  296. const Document* PageView::document() const
  297. {
  298. return page().main_frame().document();
  299. }
  300. void PageView::set_document(Document* document)
  301. {
  302. page().main_frame().set_document(document);
  303. }
  304. void PageView::did_scroll()
  305. {
  306. page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  307. page().main_frame().did_scroll({});
  308. }
  309. void PageView::drop_event(GUI::DropEvent& event)
  310. {
  311. if (event.mime_data().has_urls()) {
  312. if (on_url_drop) {
  313. on_url_drop(event.mime_data().urls().first());
  314. return;
  315. }
  316. }
  317. ScrollableWidget::drop_event(event);
  318. }
  319. }