Box.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/HTMLHtmlElement.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/FormattingContext.h>
  13. #include <LibWeb/Painting/PaintableBox.h>
  14. namespace Web::Layout {
  15. Box::Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  16. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  17. {
  18. }
  19. Box::Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  20. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  21. {
  22. }
  23. Box::~Box()
  24. {
  25. }
  26. bool Box::is_scrollable() const
  27. {
  28. // FIXME: Support horizontal scroll as well (overflow-x)
  29. return computed_values().overflow_y() == CSS::Overflow::Scroll;
  30. }
  31. void Box::set_scroll_offset(CSSPixelPoint offset)
  32. {
  33. // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
  34. if (offset.y() < 0 || m_scroll_offset == offset)
  35. return;
  36. m_scroll_offset = offset;
  37. set_needs_display();
  38. }
  39. void Box::set_needs_display()
  40. {
  41. if (paint_box())
  42. browsing_context().set_needs_display(paint_box()->absolute_rect());
  43. }
  44. bool Box::is_body() const
  45. {
  46. return dom_node() && dom_node() == document().body();
  47. }
  48. JS::GCPtr<Painting::Paintable> Box::create_paintable() const
  49. {
  50. return Painting::PaintableBox::create(*this);
  51. }
  52. Painting::PaintableBox const* Box::paint_box() const
  53. {
  54. return static_cast<Painting::PaintableBox const*>(Node::paintable());
  55. }
  56. }