LayoutTableRow.cpp 3.5 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 <LibWeb/DOM/Element.h>
  27. #include <LibWeb/Layout/LayoutTable.h>
  28. #include <LibWeb/Layout/LayoutTableCell.h>
  29. #include <LibWeb/Layout/LayoutTableRow.h>
  30. namespace Web {
  31. LayoutTableRow::LayoutTableRow(DOM::Document& document, const DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
  32. : LayoutBox(document, &element, move(style))
  33. {
  34. }
  35. LayoutTableRow::~LayoutTableRow()
  36. {
  37. }
  38. void LayoutTableRow::layout(LayoutMode)
  39. {
  40. }
  41. void LayoutTableRow::calculate_column_widths(Vector<float>& column_widths)
  42. {
  43. size_t column_index = 0;
  44. auto* table = first_ancestor_of_type<LayoutTable>();
  45. bool use_auto_layout = !table || table->style().width().is_undefined_or_auto();
  46. for_each_child_of_type<LayoutTableCell>([&](auto& cell) {
  47. if (use_auto_layout) {
  48. cell.layout(LayoutMode::OnlyRequiredLineBreaks);
  49. } else {
  50. cell.layout(LayoutMode::Default);
  51. }
  52. column_widths[column_index] = max(column_widths[column_index], cell.width());
  53. column_index += cell.colspan();
  54. });
  55. }
  56. void LayoutTableRow::layout_row(const Vector<float>& column_widths)
  57. {
  58. size_t column_index = 0;
  59. float tallest_cell_height = 0;
  60. float content_width = 0;
  61. auto* table = first_ancestor_of_type<LayoutTable>();
  62. bool use_auto_layout = !table || table->style().width().is_undefined_or_auto();
  63. for_each_child_of_type<LayoutTableCell>([&](auto& cell) {
  64. cell.set_offset(effective_offset().translated(content_width, 0));
  65. // Layout the cell contents a second time, now that we know its final width.
  66. if (use_auto_layout) {
  67. cell.layout_inside(LayoutMode::OnlyRequiredLineBreaks);
  68. } else {
  69. cell.layout_inside(LayoutMode::Default);
  70. }
  71. size_t cell_colspan = cell.colspan();
  72. for (size_t i = 0; i < cell_colspan; ++i)
  73. content_width += column_widths[column_index++];
  74. tallest_cell_height = max(tallest_cell_height, cell.height());
  75. });
  76. if (use_auto_layout) {
  77. set_width(content_width);
  78. } else {
  79. set_width(table->width());
  80. }
  81. set_height(tallest_cell_height);
  82. }
  83. }