HtmlView.cpp 5.0 KB

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