HtmlView.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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]() {
  35. m_layout_root = m_document->create_layout_tree(m_document->style_resolver(), nullptr);
  36. layout_and_sync_size();
  37. update();
  38. };
  39. main_frame().set_document(document);
  40. if (document == nullptr)
  41. m_layout_root = nullptr;
  42. else
  43. m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
  44. #ifdef HTML_DEBUG
  45. if (document != nullptr) {
  46. dbgprintf("\033[33;1mLayout tree before layout:\033[0m\n");
  47. ::dump_tree(*m_layout_root);
  48. }
  49. #endif
  50. layout_and_sync_size();
  51. update();
  52. }
  53. void HtmlView::layout_and_sync_size()
  54. {
  55. if (!m_layout_root)
  56. return;
  57. main_frame().set_size(available_size());
  58. m_layout_root->layout();
  59. set_content_size(m_layout_root->rect().size());
  60. #ifdef HTML_DEBUG
  61. dbgprintf("\033[33;1mLayout tree after layout:\033[0m\n");
  62. ::dump_tree(*m_layout_root);
  63. #endif
  64. }
  65. void HtmlView::resize_event(GResizeEvent& event)
  66. {
  67. GScrollableWidget::resize_event(event);
  68. layout_and_sync_size();
  69. }
  70. void HtmlView::paint_event(GPaintEvent& event)
  71. {
  72. GFrame::paint_event(event);
  73. GPainter painter(*this);
  74. painter.add_clip_rect(widget_inner_rect());
  75. painter.add_clip_rect(event.rect());
  76. if (!m_layout_root) {
  77. painter.fill_rect(event.rect(), background_color());
  78. return;
  79. }
  80. painter.fill_rect(event.rect(), m_document->background_color());
  81. painter.translate(frame_thickness(), frame_thickness());
  82. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  83. RenderingContext context { painter };
  84. m_layout_root->render(context);
  85. }
  86. void HtmlView::mousemove_event(GMouseEvent& event)
  87. {
  88. if (!m_layout_root)
  89. return GScrollableWidget::mousemove_event(event);
  90. bool hovered_node_changed = false;
  91. auto result = m_layout_root->hit_test(to_content_position(event.position()));
  92. if (result.layout_node) {
  93. auto* node = result.layout_node->node();
  94. hovered_node_changed = node != m_document->hovered_node();
  95. m_document->set_hovered_node(const_cast<Node*>(node));
  96. #ifdef HTML_DEBUG
  97. if (node) {
  98. if (auto* link = node->enclosing_link_element()) {
  99. dbg() << "HtmlView: hovering over a link to " << link->href();
  100. }
  101. }
  102. #endif
  103. }
  104. if (hovered_node_changed) {
  105. update();
  106. auto* hovered_html_element = m_document->hovered_node() ? m_document->hovered_node()->enclosing_html_element() : nullptr;
  107. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  108. auto screen_position = screen_relative_rect().location().translated(event.position());
  109. GApplication::the().show_tooltip(hovered_html_element->title(), screen_position.translated(4, 4));
  110. } else {
  111. GApplication::the().hide_tooltip();
  112. }
  113. }
  114. event.accept();
  115. }
  116. void HtmlView::mousedown_event(GMouseEvent& event)
  117. {
  118. if (!m_layout_root)
  119. return GScrollableWidget::mousemove_event(event);
  120. bool hovered_node_changed = false;
  121. auto result = m_layout_root->hit_test(to_content_position(event.position()));
  122. if (result.layout_node) {
  123. auto* node = result.layout_node->node();
  124. hovered_node_changed = node != m_document->hovered_node();
  125. m_document->set_hovered_node(const_cast<Node*>(node));
  126. if (node) {
  127. if (auto* link = node->enclosing_link_element()) {
  128. dbg() << "HtmlView: clicking on a link to " << link->href();
  129. if (on_link_click)
  130. on_link_click(link->href());
  131. }
  132. }
  133. }
  134. if (hovered_node_changed)
  135. update();
  136. event.accept();
  137. }
  138. void HtmlView::reload()
  139. {
  140. load(main_frame().document()->url());
  141. }
  142. void HtmlView::load(const URL& url)
  143. {
  144. dbg() << "HtmlView::load: " << url;
  145. if (on_load_start)
  146. on_load_start(url);
  147. auto f = CFile::construct();
  148. f->set_filename(url.path());
  149. if (!f->open(CIODevice::OpenMode::ReadOnly)) {
  150. dbg() << "HtmlView::load: Error: " << f->error_string();
  151. return;
  152. }
  153. String html = String::copy(f->read_all());
  154. auto document = parse_html(html, url);
  155. document->normalize();
  156. set_document(document);
  157. if (on_title_change)
  158. on_title_change(document->title());
  159. }