Grid.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "Grid.h"
  6. #include "GridArea.h"
  7. #include "GridDimension.h"
  8. #include "mozilla/dom/GridBinding.h"
  9. #include "nsGridContainerFrame.h"
  10. namespace mozilla {
  11. namespace dom {
  12. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Grid, mParent, mRows, mCols, mAreas)
  13. NS_IMPL_CYCLE_COLLECTING_ADDREF(Grid)
  14. NS_IMPL_CYCLE_COLLECTING_RELEASE(Grid)
  15. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Grid)
  16. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  17. NS_INTERFACE_MAP_ENTRY(nsISupports)
  18. NS_INTERFACE_MAP_END
  19. Grid::Grid(nsISupports* aParent,
  20. nsGridContainerFrame* aFrame)
  21. : mParent(do_QueryInterface(aParent))
  22. , mRows(new GridDimension(this))
  23. , mCols(new GridDimension(this))
  24. {
  25. MOZ_ASSERT(aFrame,
  26. "Should never be instantiated with a null nsGridContainerFrame");
  27. // Construct areas first, because lines may need to reference them
  28. // to extract additional names for boundary lines.
  29. // Add implicit areas first. Track the names that we add here, because
  30. // we will ignore future explicit areas with the same name.
  31. nsTHashtable<nsStringHashKey> namesSeen;
  32. nsGridContainerFrame::ImplicitNamedAreas* implicitAreas =
  33. aFrame->GetImplicitNamedAreas();
  34. if (implicitAreas) {
  35. for (auto iter = implicitAreas->Iter(); !iter.Done(); iter.Next()) {
  36. auto& areaInfo = iter.Data();
  37. namesSeen.PutEntry(areaInfo.mName);
  38. GridArea* area = new GridArea(this,
  39. areaInfo.mName,
  40. GridDeclaration::Implicit,
  41. areaInfo.mRowStart,
  42. areaInfo.mRowEnd,
  43. areaInfo.mColumnStart,
  44. areaInfo.mColumnEnd);
  45. mAreas.AppendElement(area);
  46. }
  47. }
  48. // Add explicit areas next, as long as they don't have the same name
  49. // as the implicit areas, because the implicit values override what was
  50. // initially available in the explicit areas.
  51. nsGridContainerFrame::ExplicitNamedAreas* explicitAreas =
  52. aFrame->GetExplicitNamedAreas();
  53. if (explicitAreas) {
  54. for (auto& areaInfo : *explicitAreas) {
  55. if (!namesSeen.Contains(areaInfo.mName)) {
  56. GridArea* area = new GridArea(this,
  57. areaInfo.mName,
  58. GridDeclaration::Explicit,
  59. areaInfo.mRowStart,
  60. areaInfo.mRowEnd,
  61. areaInfo.mColumnStart,
  62. areaInfo.mColumnEnd);
  63. mAreas.AppendElement(area);
  64. }
  65. }
  66. }
  67. // Now construct the tracks and lines.
  68. const ComputedGridTrackInfo* rowTrackInfo =
  69. aFrame->GetComputedTemplateRows();
  70. const ComputedGridLineInfo* rowLineInfo =
  71. aFrame->GetComputedTemplateRowLines();
  72. mRows->SetTrackInfo(rowTrackInfo);
  73. mRows->SetLineInfo(rowTrackInfo, rowLineInfo, mAreas, true);
  74. const ComputedGridTrackInfo* columnTrackInfo =
  75. aFrame->GetComputedTemplateColumns();
  76. const ComputedGridLineInfo* columnLineInfo =
  77. aFrame->GetComputedTemplateColumnLines();
  78. mCols->SetTrackInfo(columnTrackInfo);
  79. mCols->SetLineInfo(columnTrackInfo, columnLineInfo, mAreas, false);
  80. }
  81. Grid::~Grid()
  82. {
  83. }
  84. JSObject*
  85. Grid::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  86. {
  87. return GridBinding::Wrap(aCx, this, aGivenProto);
  88. }
  89. GridDimension*
  90. Grid::Rows() const
  91. {
  92. return mRows;
  93. }
  94. GridDimension*
  95. Grid::Cols() const
  96. {
  97. return mCols;
  98. }
  99. void
  100. Grid::GetAreas(nsTArray<RefPtr<GridArea>>& aAreas) const
  101. {
  102. aAreas = mAreas;
  103. }
  104. } // namespace dom
  105. } // namespace mozilla