layout/xul/grid/nsGridRowGroupLayout.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/xul/grid/nsGridRowGroupLayout.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,264 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +//
    1.10 +// Eric Vaughan
    1.11 +// Netscape Communications
    1.12 +//
    1.13 +// See documentation in associated header file
    1.14 +//
    1.15 +
    1.16 +
    1.17 +/*
    1.18 + * The nsGridRowGroupLayout implements the <rows> or <columns> tag in a grid. 
    1.19 + */
    1.20 +
    1.21 +#include "nsGridRowGroupLayout.h"
    1.22 +#include "nsCOMPtr.h"
    1.23 +#include "nsIScrollableFrame.h"
    1.24 +#include "nsBoxLayoutState.h"
    1.25 +#include "nsGridLayout2.h"
    1.26 +#include "nsGridRow.h"
    1.27 +#include "nsHTMLReflowState.h"
    1.28 +
    1.29 +already_AddRefed<nsBoxLayout> NS_NewGridRowGroupLayout()
    1.30 +{
    1.31 +  nsRefPtr<nsBoxLayout> layout = new nsGridRowGroupLayout();
    1.32 +  return layout.forget();
    1.33 +} 
    1.34 +
    1.35 +nsGridRowGroupLayout::nsGridRowGroupLayout():nsGridRowLayout(), mRowCount(0)
    1.36 +{
    1.37 +}
    1.38 +
    1.39 +nsGridRowGroupLayout::~nsGridRowGroupLayout()
    1.40 +{
    1.41 +}
    1.42 +
    1.43 +void
    1.44 +nsGridRowGroupLayout::ChildAddedOrRemoved(nsIFrame* aBox, nsBoxLayoutState& aState)
    1.45 +{
    1.46 +  int32_t index = 0;
    1.47 +  nsGrid* grid = GetGrid(aBox, &index);
    1.48 +  bool isHorizontal = IsHorizontal(aBox);
    1.49 +
    1.50 +  if (grid)
    1.51 +    grid->RowAddedOrRemoved(aState, index, isHorizontal);
    1.52 +}
    1.53 +
    1.54 +void
    1.55 +nsGridRowGroupLayout::AddWidth(nsSize& aSize, nscoord aSize2, bool aIsHorizontal)
    1.56 +{
    1.57 +  nscoord& size = GET_WIDTH(aSize, aIsHorizontal);
    1.58 +
    1.59 +  if (size == NS_INTRINSICSIZE || aSize2 == NS_INTRINSICSIZE)
    1.60 +    size = NS_INTRINSICSIZE;
    1.61 +  else
    1.62 +    size += aSize2;
    1.63 +}
    1.64 +
    1.65 +nsSize
    1.66 +nsGridRowGroupLayout::GetPrefSize(nsIFrame* aBox, nsBoxLayoutState& aState)
    1.67 +{ 
    1.68 +  nsSize vpref = nsGridRowLayout::GetPrefSize(aBox, aState); 
    1.69 +
    1.70 +
    1.71 + /* It is possible that we could have some extra columns. This is when less columns in XUL were 
    1.72 +  * defined that needed. And example might be a grid with 3 defined columns but a row with 4 cells in 
    1.73 +  * it. We would need an extra column to make the grid work. But because that extra column does not 
    1.74 +  * have a box associated with it we must add its size in manually. Remember we could have extra rows
    1.75 +  * as well.
    1.76 +  */
    1.77 +
    1.78 +  int32_t index = 0;
    1.79 +  nsGrid* grid = GetGrid(aBox, &index);
    1.80 +
    1.81 +  if (grid) 
    1.82 +  {
    1.83 +    // make sure we add in extra columns sizes as well
    1.84 +    bool isHorizontal = IsHorizontal(aBox);
    1.85 +    int32_t extraColumns = grid->GetExtraColumnCount(isHorizontal);
    1.86 +    int32_t start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
    1.87 +    for (int32_t i=0; i < extraColumns; i++)
    1.88 +    {
    1.89 +      nscoord pref =
    1.90 +        grid->GetPrefRowHeight(aState, i+start, !isHorizontal); // GetPrefColumnWidth
    1.91 +
    1.92 +      AddWidth(vpref, pref, isHorizontal);
    1.93 +    }
    1.94 +  }
    1.95 +
    1.96 +  return vpref;
    1.97 +}
    1.98 +
    1.99 +nsSize
   1.100 +nsGridRowGroupLayout::GetMaxSize(nsIFrame* aBox, nsBoxLayoutState& aState)
   1.101 +{
   1.102 + nsSize maxSize = nsGridRowLayout::GetMaxSize(aBox, aState); 
   1.103 +
   1.104 +  int32_t index = 0;
   1.105 +  nsGrid* grid = GetGrid(aBox, &index);
   1.106 +
   1.107 +  if (grid) 
   1.108 +  {
   1.109 +    // make sure we add in extra columns sizes as well
   1.110 +    bool isHorizontal = IsHorizontal(aBox);
   1.111 +    int32_t extraColumns = grid->GetExtraColumnCount(isHorizontal);
   1.112 +    int32_t start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
   1.113 +    for (int32_t i=0; i < extraColumns; i++)
   1.114 +    {
   1.115 +      nscoord max =
   1.116 +        grid->GetMaxRowHeight(aState, i+start, !isHorizontal); // GetMaxColumnWidth
   1.117 +
   1.118 +      AddWidth(maxSize, max, isHorizontal);
   1.119 +    }
   1.120 +  }
   1.121 +
   1.122 +  return maxSize;
   1.123 +}
   1.124 +
   1.125 +nsSize
   1.126 +nsGridRowGroupLayout::GetMinSize(nsIFrame* aBox, nsBoxLayoutState& aState)
   1.127 +{
   1.128 +  nsSize minSize = nsGridRowLayout::GetMinSize(aBox, aState); 
   1.129 +
   1.130 +  int32_t index = 0;
   1.131 +  nsGrid* grid = GetGrid(aBox, &index);
   1.132 +
   1.133 +  if (grid) 
   1.134 +  {
   1.135 +    // make sure we add in extra columns sizes as well
   1.136 +    bool isHorizontal = IsHorizontal(aBox);
   1.137 +    int32_t extraColumns = grid->GetExtraColumnCount(isHorizontal);
   1.138 +    int32_t start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
   1.139 +    for (int32_t i=0; i < extraColumns; i++)
   1.140 +    {
   1.141 +      nscoord min = 
   1.142 +        grid->GetMinRowHeight(aState, i+start, !isHorizontal); // GetMinColumnWidth
   1.143 +      AddWidth(minSize, min, isHorizontal);
   1.144 +    }
   1.145 +  }
   1.146 +
   1.147 +  return minSize;
   1.148 +}
   1.149 +
   1.150 +/*
   1.151 + * Run down through our children dirtying them recursively.
   1.152 + */
   1.153 +void
   1.154 +nsGridRowGroupLayout::DirtyRows(nsIFrame* aBox, nsBoxLayoutState& aState)
   1.155 +{
   1.156 +  if (aBox) {
   1.157 +    // mark us dirty
   1.158 +    // XXXldb We probably don't want to walk up the ancestor chain
   1.159 +    // calling MarkIntrinsicWidthsDirty for every row group.
   1.160 +    aState.PresShell()->FrameNeedsReflow(aBox, nsIPresShell::eTreeChange,
   1.161 +                                         NS_FRAME_IS_DIRTY);
   1.162 +    nsIFrame* child = aBox->GetChildBox();
   1.163 +
   1.164 +    while(child) {
   1.165 +
   1.166 +      // walk into scrollframes
   1.167 +      nsIFrame* deepChild = nsGrid::GetScrolledBox(child);
   1.168 +
   1.169 +      // walk into other monuments
   1.170 +      nsIGridPart* monument = nsGrid::GetPartFromBox(deepChild);
   1.171 +      if (monument) 
   1.172 +        monument->DirtyRows(deepChild, aState);
   1.173 +
   1.174 +      child = child->GetNextBox();
   1.175 +    }
   1.176 +  }
   1.177 +}
   1.178 +
   1.179 +
   1.180 +void
   1.181 +nsGridRowGroupLayout::CountRowsColumns(nsIFrame* aBox, int32_t& aRowCount, int32_t& aComputedColumnCount)
   1.182 +{
   1.183 +  if (aBox) {
   1.184 +    int32_t startCount = aRowCount;
   1.185 +
   1.186 +    nsIFrame* child = aBox->GetChildBox();
   1.187 +
   1.188 +    while(child) {
   1.189 +      
   1.190 +      // first see if it is a scrollframe. If so walk down into it and get the scrolled child
   1.191 +      nsIFrame* deepChild = nsGrid::GetScrolledBox(child);
   1.192 +
   1.193 +      nsIGridPart* monument = nsGrid::GetPartFromBox(deepChild);
   1.194 +      if (monument) {
   1.195 +        monument->CountRowsColumns(deepChild, aRowCount, aComputedColumnCount);
   1.196 +        child = child->GetNextBox();
   1.197 +        deepChild = child;
   1.198 +        continue;
   1.199 +      }
   1.200 +
   1.201 +      child = child->GetNextBox();
   1.202 +
   1.203 +      // if not a monument. Then count it. It will be a bogus row
   1.204 +      aRowCount++;
   1.205 +    }
   1.206 +
   1.207 +    mRowCount = aRowCount - startCount;
   1.208 +  }
   1.209 +}
   1.210 +
   1.211 +
   1.212 +/**
   1.213 + * Fill out the given row structure recursively
   1.214 + */
   1.215 +int32_t 
   1.216 +nsGridRowGroupLayout::BuildRows(nsIFrame* aBox, nsGridRow* aRows)
   1.217 +{ 
   1.218 +  int32_t rowCount = 0;
   1.219 +
   1.220 +  if (aBox) {
   1.221 +    nsIFrame* child = aBox->GetChildBox();
   1.222 +
   1.223 +    while(child) {
   1.224 +      
   1.225 +      // first see if it is a scrollframe. If so walk down into it and get the scrolled child
   1.226 +      nsIFrame* deepChild = nsGrid::GetScrolledBox(child);
   1.227 +
   1.228 +      nsIGridPart* monument = nsGrid::GetPartFromBox(deepChild);
   1.229 +      if (monument) {
   1.230 +        rowCount += monument->BuildRows(deepChild, &aRows[rowCount]);
   1.231 +        child = child->GetNextBox();
   1.232 +        deepChild = child;
   1.233 +        continue;
   1.234 +      }
   1.235 +
   1.236 +      aRows[rowCount].Init(child, true);
   1.237 +
   1.238 +      child = child->GetNextBox();
   1.239 +
   1.240 +      // if not a monument. Then count it. It will be a bogus row
   1.241 +      rowCount++;
   1.242 +    }
   1.243 +  }
   1.244 +
   1.245 +  return rowCount;
   1.246 +}
   1.247 +
   1.248 +nsMargin
   1.249 +nsGridRowGroupLayout::GetTotalMargin(nsIFrame* aBox, bool aIsHorizontal)
   1.250 +{
   1.251 +  // group have border and padding added to the total margin
   1.252 +
   1.253 +  nsMargin margin = nsGridRowLayout::GetTotalMargin(aBox, aIsHorizontal);
   1.254 +  
   1.255 +  // make sure we have the scrollframe on the outside if it has one.
   1.256 +  // that's where the border is.
   1.257 +  aBox = nsGrid::GetScrollBox(aBox);
   1.258 +
   1.259 +  // add our border/padding to it
   1.260 +  nsMargin borderPadding(0,0,0,0);
   1.261 +  aBox->GetBorderAndPadding(borderPadding);
   1.262 +  margin += borderPadding;
   1.263 +
   1.264 +  return margin;
   1.265 +}
   1.266 +
   1.267 +

mercurial