LayoutFrame.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <LibGUI/Painter.h>
  27. #include <LibGUI/ScrollBar.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/StylePainter.h>
  31. #include <LibWeb/DOM/Document.h>
  32. #include <LibWeb/Frame/Frame.h>
  33. #include <LibWeb/Layout/LayoutDocument.h>
  34. #include <LibWeb/Layout/LayoutFrame.h>
  35. #include <LibWeb/PageView.h>
  36. namespace Web {
  37. LayoutFrame::LayoutFrame(DOM::Document& document, const DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  38. : LayoutReplaced(document, element, move(style))
  39. {
  40. }
  41. LayoutFrame::~LayoutFrame()
  42. {
  43. }
  44. void LayoutFrame::layout(LayoutMode layout_mode)
  45. {
  46. ASSERT(node().hosted_frame());
  47. set_has_intrinsic_width(true);
  48. set_has_intrinsic_height(true);
  49. // FIXME: Do proper error checking, etc.
  50. set_intrinsic_width(node().attribute(HTML::AttributeNames::width).to_int().value_or(300));
  51. set_intrinsic_height(node().attribute(HTML::AttributeNames::height).to_int().value_or(150));
  52. LayoutReplaced::layout(layout_mode);
  53. }
  54. void LayoutFrame::paint(PaintContext& context, PaintPhase phase)
  55. {
  56. LayoutReplaced::paint(context, phase);
  57. if (phase == PaintPhase::Foreground) {
  58. auto* hosted_document = node().hosted_document();
  59. if (!hosted_document)
  60. return;
  61. auto* hosted_layout_tree = hosted_document->layout_node();
  62. if (!hosted_layout_tree)
  63. return;
  64. context.painter().save();
  65. auto old_viewport_rect = context.viewport_rect();
  66. context.painter().add_clip_rect(enclosing_int_rect(absolute_rect()));
  67. context.painter().translate(absolute_x(), absolute_y());
  68. context.set_viewport_rect({ {}, node().hosted_frame()->size() });
  69. const_cast<LayoutDocument*>(hosted_layout_tree)->paint_all_phases(context);
  70. context.set_viewport_rect(old_viewport_rect);
  71. context.painter().restore();
  72. }
  73. }
  74. void LayoutFrame::did_set_rect()
  75. {
  76. LayoutReplaced::did_set_rect();
  77. ASSERT(node().hosted_frame());
  78. node().hosted_frame()->set_size(size().to_int_size());
  79. }
  80. }