HtmlView.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <LibGUI/GPainter.h>
  2. #include <LibGUI/GScrollBar.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/Dump.h>
  5. #include <LibHTML/HtmlView.h>
  6. #include <LibHTML/Layout/LayoutNode.h>
  7. #include <LibHTML/RenderingContext.h>
  8. #include <stdio.h>
  9. HtmlView::HtmlView(GWidget* parent)
  10. : GScrollableWidget(parent)
  11. {
  12. set_frame_shape(FrameShape::Container);
  13. set_frame_shadow(FrameShadow::Sunken);
  14. set_frame_thickness(2);
  15. set_should_hide_unnecessary_scrollbars(true);
  16. set_background_color(Color::White);
  17. }
  18. void HtmlView::set_document(Document* document)
  19. {
  20. if (document == m_document)
  21. return;
  22. m_document = document;
  23. if (document == nullptr)
  24. m_layout_root = nullptr;
  25. else
  26. m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
  27. #ifdef HTML_DEBUG
  28. if (document != nullptr) {
  29. printf("\033[33;1mLayout tree before layout:\033[0m\n");
  30. ::dump_tree(*m_layout_root);
  31. }
  32. #endif
  33. layout_and_sync_size();
  34. update();
  35. }
  36. void HtmlView::layout_and_sync_size()
  37. {
  38. if (!m_layout_root)
  39. return;
  40. m_layout_root->style().size().set_width(available_size().width());
  41. m_layout_root->layout();
  42. set_content_size(m_layout_root->rect().size());
  43. #ifdef HTML_DEBUG
  44. printf("\033[33;1mLayout tree after layout:\033[0m\n");
  45. ::dump_tree(*m_layout_root);
  46. #endif
  47. }
  48. void HtmlView::resize_event(GResizeEvent& event)
  49. {
  50. GScrollableWidget::resize_event(event);
  51. layout_and_sync_size();
  52. }
  53. void HtmlView::paint_event(GPaintEvent& event)
  54. {
  55. GFrame::paint_event(event);
  56. GPainter painter(*this);
  57. painter.add_clip_rect(widget_inner_rect());
  58. painter.add_clip_rect(event.rect());
  59. painter.fill_rect(event.rect(), background_color());
  60. painter.translate(frame_thickness(), frame_thickness());
  61. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  62. if (!m_layout_root)
  63. return;
  64. RenderingContext context { painter };
  65. m_layout_root->render(context);
  66. }
  67. void HtmlView::mousemove_event(GMouseEvent& event)
  68. {
  69. if (!m_layout_root)
  70. return GScrollableWidget::mousemove_event(event);
  71. bool hovered_node_changed = false;
  72. auto result = m_layout_root->hit_test(event.position());
  73. if (result.layout_node) {
  74. auto* node = result.layout_node->node();
  75. m_document->set_hovered_node(const_cast<Node*>(node));
  76. hovered_node_changed = node == m_document->hovered_node();
  77. if (node) {
  78. dbg() << "HtmlView: mousemove: " << node->tag_name() << "{" << node << "}";
  79. }
  80. }
  81. if (hovered_node_changed)
  82. update();
  83. event.accept();
  84. }