LayoutFrame.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/InProcessWebView.h>
  33. #include <LibWeb/Layout/LayoutDocument.h>
  34. #include <LibWeb/Layout/LayoutFrame.h>
  35. #include <LibWeb/Page/Frame.h>
  36. //#define DEBUG_HIGHLIGHT_FOCUSED_FRAME
  37. namespace Web {
  38. LayoutFrame::LayoutFrame(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  39. : LayoutReplaced(document, element, move(style))
  40. {
  41. }
  42. LayoutFrame::~LayoutFrame()
  43. {
  44. }
  45. void LayoutFrame::prepare_for_replaced_layout()
  46. {
  47. ASSERT(node().content_frame());
  48. set_has_intrinsic_width(true);
  49. set_has_intrinsic_height(true);
  50. // FIXME: Do proper error checking, etc.
  51. set_intrinsic_width(node().attribute(HTML::AttributeNames::width).to_int().value_or(300));
  52. set_intrinsic_height(node().attribute(HTML::AttributeNames::height).to_int().value_or(150));
  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().content_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().content_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. #ifdef DEBUG_HIGHLIGHT_FOCUSED_FRAME
  73. if (node().content_frame()->is_focused_frame()) {
  74. context.painter().draw_rect(absolute_rect().to<int>(), Color::Cyan);
  75. }
  76. #endif
  77. }
  78. }
  79. void LayoutFrame::did_set_rect()
  80. {
  81. LayoutReplaced::did_set_rect();
  82. ASSERT(node().content_frame());
  83. node().content_frame()->set_size(size().to_type<int>());
  84. }
  85. }