HtmlView.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <LibGUI/GPainter.h>
  2. #include <LibGUI/GScrollBar.h>
  3. #include <LibHTML/Dump.h>
  4. #include <LibHTML/HtmlView.h>
  5. #include <LibHTML/Layout/LayoutNode.h>
  6. #include <LibHTML/RenderingContext.h>
  7. #include <stdio.h>
  8. HtmlView::HtmlView(GWidget* parent)
  9. : GScrollableWidget(parent)
  10. {
  11. set_frame_shape(FrameShape::Container);
  12. set_frame_shadow(FrameShadow::Sunken);
  13. set_frame_thickness(2);
  14. set_should_hide_unnecessary_scrollbars(true);
  15. set_background_color(Color::White);
  16. }
  17. void HtmlView::set_document(Document* document)
  18. {
  19. if (document == m_document)
  20. return;
  21. m_document = document;
  22. if (document == nullptr)
  23. m_layout_root = nullptr;
  24. else
  25. m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
  26. #ifdef HTML_DEBUG
  27. if (document != nullptr) {
  28. printf("\033[33;1mLayout tree before layout:\033[0m\n");
  29. ::dump_tree(*m_layout_root);
  30. }
  31. #endif
  32. layout_and_sync_size();
  33. update();
  34. }
  35. void HtmlView::layout_and_sync_size()
  36. {
  37. if (!m_layout_root)
  38. return;
  39. m_layout_root->style().size().set_width(available_size().width());
  40. m_layout_root->layout();
  41. set_content_size(m_layout_root->rect().size());
  42. #ifdef HTML_DEBUG
  43. printf("\033[33;1mLayout tree after layout:\033[0m\n");
  44. ::dump_tree(*m_layout_root);
  45. #endif
  46. }
  47. void HtmlView::resize_event(GResizeEvent& event)
  48. {
  49. GScrollableWidget::resize_event(event);
  50. layout_and_sync_size();
  51. }
  52. void HtmlView::paint_event(GPaintEvent& event)
  53. {
  54. GFrame::paint_event(event);
  55. GPainter painter(*this);
  56. painter.add_clip_rect(widget_inner_rect());
  57. painter.add_clip_rect(event.rect());
  58. painter.fill_rect(event.rect(), background_color());
  59. painter.translate(frame_thickness(), frame_thickness());
  60. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  61. if (!m_layout_root)
  62. return;
  63. RenderingContext context { painter };
  64. m_layout_root->render(context);
  65. }