FrameBox.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <LibGfx/Painter.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/InProcessWebView.h>
  30. #include <LibWeb/Layout/FrameBox.h>
  31. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  32. #include <LibWeb/Page/Frame.h>
  33. namespace Web::Layout {
  34. FrameBox::FrameBox(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  35. : ReplacedBox(document, element, move(style))
  36. {
  37. }
  38. FrameBox::~FrameBox()
  39. {
  40. }
  41. void FrameBox::prepare_for_replaced_layout()
  42. {
  43. VERIFY(dom_node().content_frame());
  44. set_has_intrinsic_width(true);
  45. set_has_intrinsic_height(true);
  46. // FIXME: Do proper error checking, etc.
  47. set_intrinsic_width(dom_node().attribute(HTML::AttributeNames::width).to_int().value_or(300));
  48. set_intrinsic_height(dom_node().attribute(HTML::AttributeNames::height).to_int().value_or(150));
  49. }
  50. void FrameBox::paint(PaintContext& context, PaintPhase phase)
  51. {
  52. ReplacedBox::paint(context, phase);
  53. if (phase == PaintPhase::Foreground) {
  54. auto* hosted_document = dom_node().content_document();
  55. if (!hosted_document)
  56. return;
  57. auto* hosted_layout_tree = hosted_document->layout_node();
  58. if (!hosted_layout_tree)
  59. return;
  60. context.painter().save();
  61. auto old_viewport_rect = context.viewport_rect();
  62. context.painter().add_clip_rect(enclosing_int_rect(absolute_rect()));
  63. context.painter().translate(absolute_x(), absolute_y());
  64. context.set_viewport_rect({ {}, dom_node().content_frame()->size() });
  65. const_cast<Layout::InitialContainingBlockBox*>(hosted_layout_tree)->paint_all_phases(context);
  66. context.set_viewport_rect(old_viewport_rect);
  67. context.painter().restore();
  68. #if HIGHLIGHT_FOCUSED_FRAME_DEBUG
  69. if (dom_node().content_frame()->is_focused_frame()) {
  70. context.painter().draw_rect(absolute_rect().to<int>(), Color::Cyan);
  71. }
  72. #endif
  73. }
  74. }
  75. void FrameBox::did_set_rect()
  76. {
  77. ReplacedBox::did_set_rect();
  78. VERIFY(dom_node().content_frame());
  79. dom_node().content_frame()->set_size(size().to_type<int>());
  80. }
  81. }