FrameBox.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/Layout/FrameBox.h>
  10. #include <LibWeb/Layout/InitialContainingBlock.h>
  11. #include <LibWeb/Page/BrowsingContext.h>
  12. namespace Web::Layout {
  13. FrameBox::FrameBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  14. : ReplacedBox(document, element, move(style))
  15. {
  16. }
  17. FrameBox::~FrameBox()
  18. {
  19. }
  20. void FrameBox::prepare_for_replaced_layout()
  21. {
  22. VERIFY(dom_node().nested_browsing_context());
  23. set_has_intrinsic_width(true);
  24. set_has_intrinsic_height(true);
  25. // FIXME: Do proper error checking, etc.
  26. set_intrinsic_width(dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(300));
  27. set_intrinsic_height(dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(150));
  28. }
  29. void FrameBox::paint(PaintContext& context, PaintPhase phase)
  30. {
  31. ReplacedBox::paint(context, phase);
  32. if (phase == PaintPhase::Foreground) {
  33. auto* hosted_document = dom_node().content_document();
  34. if (!hosted_document)
  35. return;
  36. auto* hosted_layout_tree = hosted_document->layout_node();
  37. if (!hosted_layout_tree)
  38. return;
  39. context.painter().save();
  40. auto old_viewport_rect = context.viewport_rect();
  41. context.painter().add_clip_rect(enclosing_int_rect(absolute_rect()));
  42. context.painter().translate(absolute_x(), absolute_y());
  43. context.set_viewport_rect({ {}, dom_node().nested_browsing_context()->size() });
  44. const_cast<Layout::InitialContainingBlock*>(hosted_layout_tree)->paint_all_phases(context);
  45. context.set_viewport_rect(old_viewport_rect);
  46. context.painter().restore();
  47. if constexpr (HIGHLIGHT_FOCUSED_FRAME_DEBUG) {
  48. if (dom_node().nested_browsing_context()->is_focused_context()) {
  49. context.painter().draw_rect(absolute_rect().to_type<int>(), Color::Cyan);
  50. }
  51. }
  52. }
  53. }
  54. void FrameBox::did_set_rect()
  55. {
  56. ReplacedBox::did_set_rect();
  57. VERIFY(dom_node().nested_browsing_context());
  58. dom_node().nested_browsing_context()->set_size(size().to_type<int>());
  59. }
  60. }