PageView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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/Action.h>
  31. #include <LibGUI/Application.h>
  32. #include <LibGUI/Clipboard.h>
  33. #include <LibGUI/Painter.h>
  34. #include <LibGUI/ScrollBar.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGfx/ImageDecoder.h>
  37. #include <LibJS/Runtime/Value.h>
  38. #include <LibWeb/DOM/Element.h>
  39. #include <LibWeb/DOM/ElementFactory.h>
  40. #include <LibWeb/DOM/Text.h>
  41. #include <LibWeb/Dump.h>
  42. #include <LibWeb/HTML/HTMLAnchorElement.h>
  43. #include <LibWeb/HTML/HTMLImageElement.h>
  44. #include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
  45. #include <LibWeb/Layout/LayoutBreak.h>
  46. #include <LibWeb/Layout/LayoutDocument.h>
  47. #include <LibWeb/Layout/LayoutNode.h>
  48. #include <LibWeb/Layout/LayoutText.h>
  49. #include <LibWeb/Loader/ResourceLoader.h>
  50. #include <LibWeb/Page/EventHandler.h>
  51. #include <LibWeb/Page/Frame.h>
  52. #include <LibWeb/PageView.h>
  53. #include <LibWeb/Painting/PaintContext.h>
  54. #include <LibWeb/UIEvents/MouseEvent.h>
  55. #include <stdio.h>
  56. //#define SELECTION_DEBUG
  57. namespace Web {
  58. PageView::PageView()
  59. : m_page(make<Page>(*this))
  60. {
  61. set_should_hide_unnecessary_scrollbars(true);
  62. set_background_role(ColorRole::Base);
  63. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
  64. GUI::Clipboard::the().set_data(selected_text(), "text/plain");
  65. });
  66. m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
  67. select_all();
  68. });
  69. }
  70. PageView::~PageView()
  71. {
  72. }
  73. void PageView::select_all()
  74. {
  75. auto* layout_root = this->layout_root();
  76. if (!layout_root)
  77. return;
  78. const LayoutNode* first_layout_node = layout_root;
  79. for (;;) {
  80. auto* next = first_layout_node->next_in_pre_order();
  81. if (!next)
  82. break;
  83. first_layout_node = next;
  84. if (is<LayoutText>(*first_layout_node))
  85. break;
  86. }
  87. const LayoutNode* last_layout_node = first_layout_node;
  88. for (const LayoutNode* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
  89. if (is<LayoutText>(*layout_node))
  90. last_layout_node = layout_node;
  91. }
  92. ASSERT(first_layout_node);
  93. ASSERT(last_layout_node);
  94. int last_layout_node_index_in_node = 0;
  95. if (is<LayoutText>(*last_layout_node))
  96. last_layout_node_index_in_node = downcast<LayoutText>(*last_layout_node).text_for_rendering().length() - 1;
  97. layout_root->selection().set({ first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node });
  98. update();
  99. }
  100. String PageView::selected_text() const
  101. {
  102. StringBuilder builder;
  103. auto* layout_root = this->layout_root();
  104. if (!layout_root)
  105. return {};
  106. if (!layout_root->selection().is_valid())
  107. return {};
  108. auto selection = layout_root->selection().normalized();
  109. if (selection.start().layout_node == selection.end().layout_node) {
  110. if (!is<LayoutText>(*selection.start().layout_node))
  111. return "";
  112. return downcast<LayoutText>(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node + 1);
  113. }
  114. // Start node
  115. auto layout_node = selection.start().layout_node;
  116. if (is<LayoutText>(*layout_node)) {
  117. auto& text = downcast<LayoutText>(*layout_node).text_for_rendering();
  118. builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
  119. }
  120. // Middle nodes
  121. layout_node = layout_node->next_in_pre_order();
  122. while (layout_node && layout_node != selection.end().layout_node) {
  123. if (is<LayoutText>(*layout_node))
  124. builder.append(downcast<LayoutText>(*layout_node).text_for_rendering());
  125. else if (is<LayoutBreak>(*layout_node) || is<LayoutBlock>(*layout_node))
  126. builder.append('\n');
  127. layout_node = layout_node->next_in_pre_order();
  128. }
  129. // End node
  130. ASSERT(layout_node == selection.end().layout_node);
  131. if (is<LayoutText>(*layout_node)) {
  132. auto& text = downcast<LayoutText>(*layout_node).text_for_rendering();
  133. builder.append(text.substring(0, selection.end().index_in_node + 1));
  134. }
  135. return builder.to_string();
  136. }
  137. void PageView::page_did_layout()
  138. {
  139. ASSERT(layout_root());
  140. set_content_size(layout_root()->size().to_type<int>());
  141. }
  142. void PageView::page_did_change_title(const String& title)
  143. {
  144. if (on_title_change)
  145. on_title_change(title);
  146. }
  147. void PageView::page_did_set_document_in_main_frame(DOM::Document* document)
  148. {
  149. if (on_set_document)
  150. on_set_document(document);
  151. layout_and_sync_size();
  152. scroll_to_top();
  153. update();
  154. }
  155. void PageView::page_did_start_loading(const URL& url)
  156. {
  157. if (on_load_start)
  158. on_load_start(url);
  159. }
  160. void PageView::page_did_change_selection()
  161. {
  162. update();
  163. }
  164. void PageView::page_did_request_cursor_change(GUI::StandardCursor cursor)
  165. {
  166. if (window())
  167. window()->set_override_cursor(cursor);
  168. }
  169. void PageView::page_did_request_context_menu(const Gfx::IntPoint& content_position)
  170. {
  171. if (on_context_menu_request)
  172. on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
  173. }
  174. void PageView::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
  175. {
  176. if (on_link_context_menu_request)
  177. on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
  178. }
  179. void PageView::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
  180. {
  181. if (on_link_click)
  182. on_link_click(url, target, modifiers);
  183. }
  184. void PageView::page_did_middle_click_link(const URL& url, const String& target, unsigned modifiers)
  185. {
  186. if (on_link_middle_click)
  187. on_link_middle_click(url, target, modifiers);
  188. }
  189. void PageView::page_did_enter_tooltip_area(const Gfx::IntPoint& content_position, const String& title)
  190. {
  191. GUI::Application::the()->show_tooltip(title, screen_relative_rect().location().translated(to_widget_position(content_position)));
  192. }
  193. void PageView::page_did_leave_tooltip_area()
  194. {
  195. GUI::Application::the()->hide_tooltip();
  196. }
  197. void PageView::page_did_hover_link(const URL& url)
  198. {
  199. if (on_link_hover)
  200. on_link_hover(url);
  201. }
  202. void PageView::page_did_unhover_link()
  203. {
  204. if (on_link_hover)
  205. on_link_hover({});
  206. }
  207. void PageView::page_did_invalidate(const Gfx::IntRect&)
  208. {
  209. update();
  210. }
  211. void PageView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
  212. {
  213. if (on_favicon_change)
  214. on_favicon_change(bitmap);
  215. }
  216. void PageView::layout_and_sync_size()
  217. {
  218. if (!document())
  219. return;
  220. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  221. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  222. page().main_frame().set_size(available_size());
  223. document()->layout();
  224. set_content_size(layout_root()->size().to_type<int>());
  225. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  226. // since the scrollbars now take up some of the available space.
  227. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  228. page().main_frame().set_size(available_size());
  229. document()->layout();
  230. set_content_size(layout_root()->size().to_type<int>());
  231. }
  232. page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  233. #ifdef HTML_DEBUG
  234. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  235. ::dump_tree(*layout_root());
  236. #endif
  237. }
  238. void PageView::resize_event(GUI::ResizeEvent& event)
  239. {
  240. GUI::ScrollableWidget::resize_event(event);
  241. layout_and_sync_size();
  242. }
  243. void PageView::paint_event(GUI::PaintEvent& event)
  244. {
  245. GUI::Frame::paint_event(event);
  246. GUI::Painter painter(*this);
  247. painter.add_clip_rect(widget_inner_rect());
  248. painter.add_clip_rect(event.rect());
  249. if (!layout_root()) {
  250. painter.fill_rect(event.rect(), palette().color(background_role()));
  251. return;
  252. }
  253. painter.fill_rect(event.rect(), document()->background_color(palette()));
  254. if (auto background_bitmap = document()->background_image()) {
  255. painter.draw_tiled_bitmap(event.rect(), *background_bitmap);
  256. }
  257. painter.translate(frame_thickness(), frame_thickness());
  258. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  259. PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
  260. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  261. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  262. layout_root()->paint_all_phases(context);
  263. }
  264. void PageView::mousemove_event(GUI::MouseEvent& event)
  265. {
  266. page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
  267. GUI::ScrollableWidget::mousemove_event(event);
  268. }
  269. void PageView::mousedown_event(GUI::MouseEvent& event)
  270. {
  271. page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
  272. GUI::ScrollableWidget::mousedown_event(event);
  273. }
  274. void PageView::mouseup_event(GUI::MouseEvent& event)
  275. {
  276. page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
  277. GUI::ScrollableWidget::mouseup_event(event);
  278. }
  279. void PageView::keydown_event(GUI::KeyEvent& event)
  280. {
  281. if (event.modifiers() == 0) {
  282. switch (event.key()) {
  283. case Key_Home:
  284. vertical_scrollbar().set_value(0);
  285. break;
  286. case Key_End:
  287. vertical_scrollbar().set_value(vertical_scrollbar().max());
  288. break;
  289. case Key_Down:
  290. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  291. break;
  292. case Key_Up:
  293. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  294. break;
  295. case Key_Left:
  296. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  297. break;
  298. case Key_Right:
  299. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  300. break;
  301. case Key_PageDown:
  302. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  303. break;
  304. case Key_PageUp:
  305. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. event.accept();
  312. }
  313. URL PageView::url() const
  314. {
  315. if (!page().main_frame().document())
  316. return {};
  317. return page().main_frame().document()->url();
  318. }
  319. void PageView::reload()
  320. {
  321. load(url());
  322. }
  323. void PageView::load_html(const StringView& html, const URL& url)
  324. {
  325. HTML::HTMLDocumentParser parser(html, "utf-8");
  326. parser.run(url);
  327. set_document(&parser.document());
  328. }
  329. bool PageView::load(const URL& url)
  330. {
  331. if (window())
  332. window()->set_override_cursor(GUI::StandardCursor::None);
  333. return page().main_frame().loader().load(url, FrameLoader::Type::Navigation);
  334. }
  335. const LayoutDocument* PageView::layout_root() const
  336. {
  337. return document() ? document()->layout_node() : nullptr;
  338. }
  339. LayoutDocument* PageView::layout_root()
  340. {
  341. if (!document())
  342. return nullptr;
  343. return const_cast<LayoutDocument*>(document()->layout_node());
  344. }
  345. void PageView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
  346. {
  347. scroll_into_view(rect, true, true);
  348. window()->set_override_cursor(GUI::StandardCursor::None);
  349. }
  350. void PageView::load_empty_document()
  351. {
  352. page().main_frame().set_document(nullptr);
  353. }
  354. DOM::Document* PageView::document()
  355. {
  356. return page().main_frame().document();
  357. }
  358. const DOM::Document* PageView::document() const
  359. {
  360. return page().main_frame().document();
  361. }
  362. void PageView::set_document(DOM::Document* document)
  363. {
  364. page().main_frame().set_document(document);
  365. }
  366. void PageView::did_scroll()
  367. {
  368. page().main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  369. page().main_frame().did_scroll({});
  370. }
  371. void PageView::drop_event(GUI::DropEvent& event)
  372. {
  373. if (event.mime_data().has_urls()) {
  374. if (on_url_drop) {
  375. on_url_drop(event.mime_data().urls().first());
  376. return;
  377. }
  378. }
  379. ScrollableWidget::drop_event(event);
  380. }
  381. }