FrameBox.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/InProcessWebView.h>
  10. #include <LibWeb/Layout/FrameBox.h>
  11. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  12. #include <LibWeb/Page/Frame.h>
  13. namespace Web::Layout {
  14. FrameBox::FrameBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  15. : ReplacedBox(document, element, move(style))
  16. {
  17. }
  18. FrameBox::~FrameBox()
  19. {
  20. }
  21. void FrameBox::prepare_for_replaced_layout()
  22. {
  23. VERIFY(dom_node().content_frame());
  24. set_has_intrinsic_width(true);
  25. set_has_intrinsic_height(true);
  26. // FIXME: Do proper error checking, etc.
  27. set_intrinsic_width(dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(300));
  28. set_intrinsic_height(dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(150));
  29. }
  30. void FrameBox::paint(PaintContext& context, PaintPhase phase)
  31. {
  32. ReplacedBox::paint(context, phase);
  33. if (phase == PaintPhase::Foreground) {
  34. auto* hosted_document = dom_node().content_document();
  35. if (!hosted_document)
  36. return;
  37. auto* hosted_layout_tree = hosted_document->layout_node();
  38. if (!hosted_layout_tree)
  39. return;
  40. context.painter().save();
  41. auto old_viewport_rect = context.viewport_rect();
  42. context.painter().add_clip_rect(enclosing_int_rect(absolute_rect()));
  43. context.painter().translate(absolute_x(), absolute_y());
  44. context.set_viewport_rect({ {}, dom_node().content_frame()->size() });
  45. const_cast<Layout::InitialContainingBlockBox*>(hosted_layout_tree)->paint_all_phases(context);
  46. context.set_viewport_rect(old_viewport_rect);
  47. context.painter().restore();
  48. if constexpr (HIGHLIGHT_FOCUSED_FRAME_DEBUG) {
  49. if (dom_node().content_frame()->is_focused_frame()) {
  50. context.painter().draw_rect(absolute_rect().to<int>(), Color::Cyan);
  51. }
  52. }
  53. }
  54. }
  55. void FrameBox::did_set_rect()
  56. {
  57. ReplacedBox::did_set_rect();
  58. VERIFY(dom_node().content_frame());
  59. dom_node().content_frame()->set_size(size().to_type<int>());
  60. }
  61. }