mirror of
https://github.com/RGBCube/serenity
synced 2025-05-16 18:55:07 +00:00
LibHTML: Introduce the HtmlView widget
This is a GWidget that can display contents of an HTML document. It replaces the Frame class.
This commit is contained in:
parent
8a2beaf52b
commit
b9493ba783
6 changed files with 119 additions and 70 deletions
80
Libraries/LibHTML/HtmlView.cpp
Normal file
80
Libraries/LibHTML/HtmlView.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include <LibGUI/GPainter.h>
|
||||
#include <LibGUI/GScrollBar.h>
|
||||
#include <LibHTML/Dump.h>
|
||||
#include <LibHTML/HtmlView.h>
|
||||
#include <LibHTML/Layout/LayoutNode.h>
|
||||
#include <LibHTML/RenderingContext.h>
|
||||
#include <stdio.h>
|
||||
|
||||
HtmlView::HtmlView(GWidget* parent)
|
||||
: GScrollableWidget(parent)
|
||||
{
|
||||
set_frame_shape(FrameShape::Container);
|
||||
set_frame_shadow(FrameShadow::Sunken);
|
||||
set_frame_thickness(2);
|
||||
set_should_hide_unnecessary_scrollbars(true);
|
||||
set_background_color(Color::White);
|
||||
}
|
||||
|
||||
void HtmlView::set_document(Document* document)
|
||||
{
|
||||
if (document == m_document)
|
||||
return;
|
||||
m_document = document;
|
||||
|
||||
if (document == nullptr)
|
||||
m_layout_root = nullptr;
|
||||
else
|
||||
m_layout_root = document->create_layout_tree(document->style_resolver(), nullptr);
|
||||
|
||||
#ifdef HTML_DEBUG
|
||||
if (document != nullptr) {
|
||||
printf("\033[33;1mLayout tree before layout:\033[0m\n");
|
||||
::dump_tree(*m_layout_root);
|
||||
}
|
||||
#endif
|
||||
|
||||
layout_and_sync_size();
|
||||
update();
|
||||
}
|
||||
|
||||
void HtmlView::layout_and_sync_size()
|
||||
{
|
||||
if (!m_layout_root)
|
||||
return;
|
||||
|
||||
m_layout_root->style().size().set_width(available_size().width());
|
||||
m_layout_root->layout();
|
||||
set_content_size(m_layout_root->rect().size());
|
||||
|
||||
#ifdef HTML_DEBUG
|
||||
printf("\033[33;1mLayout tree after layout:\033[0m\n");
|
||||
::dump_tree(*m_layout_root);
|
||||
#endif
|
||||
}
|
||||
|
||||
void HtmlView::resize_event(GResizeEvent& event)
|
||||
{
|
||||
GScrollableWidget::resize_event(event);
|
||||
layout_and_sync_size();
|
||||
}
|
||||
|
||||
void HtmlView::paint_event(GPaintEvent& event)
|
||||
{
|
||||
GFrame::paint_event(event);
|
||||
|
||||
GPainter painter(*this);
|
||||
painter.add_clip_rect(widget_inner_rect());
|
||||
painter.add_clip_rect(event.rect());
|
||||
|
||||
painter.fill_rect(event.rect(), background_color());
|
||||
|
||||
painter.translate(frame_thickness(), frame_thickness());
|
||||
painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
|
||||
|
||||
if (!m_layout_root)
|
||||
return;
|
||||
|
||||
RenderingContext context { painter };
|
||||
m_layout_root->render(context);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue