PageView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/RenderingContext.h>
  50. #include <stdio.h>
  51. //#define SELECTION_DEBUG
  52. namespace Web {
  53. PageView::PageView()
  54. : m_main_frame(Web::Frame::create(*this))
  55. {
  56. main_frame().on_set_document = [this](auto* document) {
  57. if (on_set_document)
  58. on_set_document(document);
  59. layout_and_sync_size();
  60. scroll_to_top();
  61. update();
  62. };
  63. main_frame().on_title_change = [this](auto& title) {
  64. if (on_title_change)
  65. on_title_change(title);
  66. };
  67. main_frame().on_load_start = [this](auto& url) {
  68. if (on_load_start)
  69. on_load_start(url);
  70. };
  71. set_should_hide_unnecessary_scrollbars(true);
  72. set_background_role(ColorRole::Base);
  73. }
  74. PageView::~PageView()
  75. {
  76. }
  77. void PageView::layout_and_sync_size()
  78. {
  79. if (!document())
  80. return;
  81. bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
  82. bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();
  83. main_frame().set_size(available_size());
  84. document()->layout();
  85. set_content_size(enclosing_int_rect(layout_root()->rect()).size());
  86. // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
  87. // since the scrollbars now take up some of the available space.
  88. if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
  89. main_frame().set_size(available_size());
  90. document()->layout();
  91. set_content_size(enclosing_int_rect(layout_root()->rect()).size());
  92. }
  93. main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  94. #ifdef HTML_DEBUG
  95. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  96. ::dump_tree(*layout_root());
  97. #endif
  98. }
  99. void PageView::resize_event(GUI::ResizeEvent& event)
  100. {
  101. GUI::ScrollableWidget::resize_event(event);
  102. layout_and_sync_size();
  103. }
  104. void PageView::paint_event(GUI::PaintEvent& event)
  105. {
  106. GUI::Frame::paint_event(event);
  107. GUI::Painter painter(*this);
  108. painter.add_clip_rect(widget_inner_rect());
  109. painter.add_clip_rect(event.rect());
  110. if (!layout_root()) {
  111. painter.fill_rect(event.rect(), palette().color(background_role()));
  112. return;
  113. }
  114. painter.fill_rect(event.rect(), document()->background_color(palette()));
  115. if (auto background_bitmap = document()->background_image()) {
  116. painter.draw_tiled_bitmap(event.rect(), *background_bitmap);
  117. }
  118. painter.translate(frame_thickness(), frame_thickness());
  119. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  120. RenderingContext context(painter, palette());
  121. context.set_should_show_line_box_borders(m_should_show_line_box_borders);
  122. context.set_viewport_rect(viewport_rect_in_content_coordinates());
  123. layout_root()->render(context);
  124. }
  125. void PageView::mousemove_event(GUI::MouseEvent& event)
  126. {
  127. if (main_frame().event_handler().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers())) {
  128. event.accept();
  129. return;
  130. }
  131. GUI::ScrollableWidget::mousemove_event(event);
  132. }
  133. void PageView::mousedown_event(GUI::MouseEvent& event)
  134. {
  135. if (main_frame().event_handler().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers())) {
  136. event.accept();
  137. return;
  138. }
  139. GUI::ScrollableWidget::mousedown_event(event);
  140. }
  141. void PageView::mouseup_event(GUI::MouseEvent& event)
  142. {
  143. if (main_frame().event_handler().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers())) {
  144. event.accept();
  145. return;
  146. }
  147. GUI::ScrollableWidget::mouseup_event(event);
  148. }
  149. void PageView::keydown_event(GUI::KeyEvent& event)
  150. {
  151. if (event.modifiers() == 0) {
  152. switch (event.key()) {
  153. case Key_Home:
  154. vertical_scrollbar().set_value(0);
  155. break;
  156. case Key_End:
  157. vertical_scrollbar().set_value(vertical_scrollbar().max());
  158. break;
  159. case Key_Down:
  160. vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
  161. break;
  162. case Key_Up:
  163. vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
  164. break;
  165. case Key_Left:
  166. horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
  167. break;
  168. case Key_Right:
  169. horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
  170. break;
  171. case Key_PageDown:
  172. vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
  173. break;
  174. case Key_PageUp:
  175. vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. event.accept();
  182. }
  183. void PageView::reload()
  184. {
  185. load(main_frame().document()->url());
  186. }
  187. bool PageView::load(const URL& url)
  188. {
  189. if (window())
  190. window()->set_override_cursor(GUI::StandardCursor::None);
  191. return main_frame().loader().load(url);
  192. }
  193. const LayoutDocument* PageView::layout_root() const
  194. {
  195. return document() ? document()->layout_node() : nullptr;
  196. }
  197. LayoutDocument* PageView::layout_root()
  198. {
  199. if (!document())
  200. return nullptr;
  201. return const_cast<LayoutDocument*>(document()->layout_node());
  202. }
  203. void PageView::scroll_to_anchor(const StringView& name)
  204. {
  205. if (!document())
  206. return;
  207. const auto* element = document()->get_element_by_id(name);
  208. if (!element) {
  209. auto candidates = document()->get_elements_by_name(name);
  210. for (auto* candidate : candidates) {
  211. if (is<HTMLAnchorElement>(*candidate)) {
  212. element = to<HTMLAnchorElement>(candidate);
  213. break;
  214. }
  215. }
  216. }
  217. if (!element) {
  218. dbg() << "PageView::scroll_to_anchor(): Anchor not found: '" << name << "'";
  219. return;
  220. }
  221. if (!element->layout_node()) {
  222. dbg() << "PageView::scroll_to_anchor(): Anchor found but without layout node: '" << name << "'";
  223. return;
  224. }
  225. auto& layout_node = *element->layout_node();
  226. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)visible_content_rect().width(), (float)visible_content_rect().height() } };
  227. scroll_into_view(enclosing_int_rect(float_rect), true, true);
  228. window()->set_override_cursor(GUI::StandardCursor::None);
  229. }
  230. void PageView::set_use_old_parser(bool use_old_parser)
  231. {
  232. main_frame().loader().set_use_old_parser(use_old_parser);
  233. }
  234. void PageView::load_empty_document()
  235. {
  236. main_frame().set_document(nullptr);
  237. }
  238. Document* PageView::document()
  239. {
  240. return main_frame().document();
  241. }
  242. const Document* PageView::document() const
  243. {
  244. return main_frame().document();
  245. }
  246. void PageView::set_document(Document* document)
  247. {
  248. main_frame().set_document(document);
  249. }
  250. void PageView::did_scroll()
  251. {
  252. main_frame().set_viewport_rect(viewport_rect_in_content_coordinates());
  253. main_frame().did_scroll({});
  254. }
  255. void PageView::drop_event(GUI::DropEvent& event)
  256. {
  257. if (event.mime_data().has_urls()) {
  258. if (on_url_drop) {
  259. on_url_drop(event.mime_data().urls().first());
  260. return;
  261. }
  262. }
  263. ScrollableWidget::drop_event(event);
  264. }
  265. void PageView::notify_link_click(Badge<EventHandler>, Web::Frame&, const String& href, const String& target, unsigned modifiers)
  266. {
  267. if (on_link_click)
  268. on_link_click(href, target, modifiers);
  269. }
  270. void PageView::notify_link_middle_click(Badge<EventHandler>, Web::Frame&, const String& href, const String&, unsigned)
  271. {
  272. if (on_link_middle_click)
  273. on_link_middle_click(href);
  274. }
  275. Gfx::Point PageView::to_screen_position(const Web::Frame& frame, const Gfx::Point& frame_position) const
  276. {
  277. Gfx::Point offset;
  278. for (auto* f = &frame; f; f = f->parent()) {
  279. auto f_position = f->host_element()->layout_node()->box_type_agnostic_position().to_int_point();
  280. offset.move_by(f_position);
  281. }
  282. return screen_relative_rect().location().translated(offset).translated(frame_position);
  283. }
  284. Gfx::Rect PageView::to_widget_rect(const Web::Frame& frame, const Gfx::Rect& frame_rect) const
  285. {
  286. Gfx::Point offset;
  287. for (auto* f = &frame; f; f = f->parent()) {
  288. if (f->is_main_frame())
  289. break;
  290. if (!f->host_element())
  291. return {};
  292. if (!f->host_element()->layout_node())
  293. return {};
  294. auto f_position = f->host_element()->layout_node()->box_type_agnostic_position().to_int_point();
  295. offset.move_by(f_position);
  296. }
  297. auto content_position = frame_rect.location().translated(offset);
  298. return { to_widget_position(content_position), frame_rect.size() };
  299. }
  300. void PageView::notify_link_context_menu_request(Badge<EventHandler>, Web::Frame& frame, const Gfx::Point& content_position, const String& href, const String&, unsigned)
  301. {
  302. if (on_link_context_menu_request)
  303. on_link_context_menu_request(href, to_screen_position(frame, content_position));
  304. }
  305. void PageView::notify_link_hover(Badge<EventHandler>, Web::Frame&, const String& href)
  306. {
  307. if (on_link_hover)
  308. on_link_hover(href);
  309. }
  310. void PageView::notify_tooltip_area_enter(Badge<EventHandler>, Web::Frame& frame, const Gfx::Point& content_position, const String& title)
  311. {
  312. GUI::Application::the().show_tooltip(title, to_screen_position(frame, content_position));
  313. }
  314. void PageView::notify_tooltip_area_leave(Badge<EventHandler>, Web::Frame&)
  315. {
  316. GUI::Application::the().hide_tooltip();
  317. }
  318. void PageView::notify_needs_display(Badge<Web::Frame>, Web::Frame& frame, const Gfx::Rect& rect)
  319. {
  320. update(to_widget_rect(frame, rect));
  321. // FIXME: This is a total hack that forces a full repaint every time.
  322. // We shouldn't have to do this, but until the ICB is actually viewport-sized, we have no choice.
  323. update();
  324. }
  325. }