HtmlView.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <LibCore/CFile.h>
  2. #include <LibGUI/GApplication.h>
  3. #include <LibGUI/GPainter.h>
  4. #include <LibGUI/GScrollBar.h>
  5. #include <LibHTML/DOM/Element.h>
  6. #include <LibHTML/DOM/HTMLAnchorElement.h>
  7. #include <LibHTML/Dump.h>
  8. #include <LibHTML/Frame.h>
  9. #include <LibHTML/HtmlView.h>
  10. #include <LibHTML/Layout/LayoutNode.h>
  11. #include <LibHTML/Parser/HTMLParser.h>
  12. #include <LibHTML/RenderingContext.h>
  13. #include <stdio.h>
  14. HtmlView::HtmlView(GWidget* parent)
  15. : GScrollableWidget(parent)
  16. , m_main_frame(Frame::create())
  17. {
  18. set_frame_shape(FrameShape::Container);
  19. set_frame_shadow(FrameShadow::Sunken);
  20. set_frame_thickness(2);
  21. set_should_hide_unnecessary_scrollbars(true);
  22. set_background_color(Color::White);
  23. }
  24. HtmlView::~HtmlView()
  25. {
  26. }
  27. void HtmlView::set_document(Document* document)
  28. {
  29. if (document == m_document)
  30. return;
  31. if (m_document)
  32. m_document->on_invalidate_layout = nullptr;
  33. m_document = document;
  34. m_document->on_invalidate_layout = [this]() { layout_and_sync_size(); };
  35. main_frame().set_document(document);
  36. if (document == nullptr)
  37. m_layout_root = nullptr;
  38. else
  39. m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
  40. #ifdef HTML_DEBUG
  41. if (document != nullptr) {
  42. dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n");
  43. ::dump_tree(*m_layout_root);
  44. }
  45. #endif
  46. layout_and_sync_size();
  47. update();
  48. }
  49. void HtmlView::layout_and_sync_size()
  50. {
  51. if (!m_layout_root)
  52. return;
  53. main_frame().set_size(available_size());
  54. m_layout_root->layout();
  55. set_content_size(m_layout_root->rect().size());
  56. #ifdef HTML_DEBUG
  57. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  58. ::dump_tree(*m_layout_root);
  59. #endif
  60. }
  61. void HtmlView::resize_event(GResizeEvent& event)
  62. {
  63. GScrollableWidget::resize_event(event);
  64. layout_and_sync_size();
  65. }
  66. void HtmlView::paint_event(GPaintEvent& event)
  67. {
  68. GFrame::paint_event(event);
  69. GPainter painter(*this);
  70. painter.add_clip_rect(widget_inner_rect());
  71. painter.add_clip_rect(event.rect());
  72. if (!m_layout_root) {
  73. painter.fill_rect(event.rect(), background_color());
  74. return;
  75. }
  76. painter.fill_rect(event.rect(), m_document->background_color());
  77. painter.translate(frame_thickness(), frame_thickness());
  78. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  79. RenderingContext context { painter };
  80. m_layout_root->render(context);
  81. }
  82. void HtmlView::mousemove_event(GMouseEvent& event)
  83. {
  84. if (!m_layout_root)
  85. return GScrollableWidget::mousemove_event(event);
  86. bool hovered_node_changed = false;
  87. auto result = m_layout_root->hit_test(to_content_position(event.position()));
  88. if (result.layout_node) {
  89. auto* node = result.layout_node->node();
  90. hovered_node_changed = node != m_document->hovered_node();
  91. m_document->set_hovered_node(const_cast<Node*>(node));
  92. #ifdef HTML_DEBUG
  93. if (node) {
  94. if (auto* link = node->enclosing_link_element()) {
  95. dbg() << "HtmlView: hovering over a link to " << link->href();
  96. }
  97. }
  98. #endif
  99. }
  100. if (hovered_node_changed) {
  101. update();
  102. auto* hovered_html_element = m_document->hovered_node() ? m_document->hovered_node()->enclosing_html_element() : nullptr;
  103. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  104. auto screen_position = screen_relative_rect().location().translated(event.position());
  105. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  106. } else {
  107. GApplication::the().hide_tooltip();
  108. }
  109. }
  110. event.accept();
  111. }
  112. void HtmlView::mousedown_event(GMouseEvent& event)
  113. {
  114. if (!m_layout_root)
  115. return GScrollableWidget::mousemove_event(event);
  116. bool hovered_node_changed = false;
  117. auto result = m_layout_root->hit_test(to_content_position(event.position()));
  118. if (result.layout_node) {
  119. auto* node = result.layout_node->node();
  120. hovered_node_changed = node != m_document->hovered_node();
  121. m_document->set_hovered_node(const_cast<Node*>(node));
  122. if (node) {
  123. if (auto* link = node->enclosing_link_element()) {
  124. dbg() << "HtmlView: clicking on a link to " << link->href();
  125. if (on_link_click)
  126. on_link_click(link->href());
  127. }
  128. }
  129. }
  130. if (hovered_node_changed)
  131. update();
  132. event.accept();
  133. }
  134. void HtmlView::reload()
  135. {
  136. load(main_frame().document()->url());
  137. }
  138. void HtmlView::load(const URL& url)
  139. {
  140. dbg() << "HtmlView::load: " << url;
  141. if (on_load_start)
  142. on_load_start(url);
  143. auto f = CFile::construct();
  144. f->set_filename(url.path());
  145. if (!f->open(CIODevice::OpenMode::ReadOnly)) {
  146. dbg() << "HtmlView::load: Error: " << f->error_string();
  147. return;
  148. }
  149. String html = String::copy(f->read_all());
  150. auto document = parse_html(html, url);
  151. document->normalize();
  152. set_document(document);
  153. if (on_title_change)
  154. on_title_change(document->title());
  155. }