summaryrefslogtreecommitdiff
path: root/layout/xul/grid/nsGridRowGroupLayout.cpp
blob: 1c600cef59818edd51428984a49335abde5e3f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//
// Eric Vaughan
// Netscape Communications
//
// See documentation in associated header file
//


/*
 * The nsGridRowGroupLayout implements the <rows> or <columns> tag in a grid. 
 */

#include "nsGridRowGroupLayout.h"
#include "nsCOMPtr.h"
#include "nsIScrollableFrame.h"
#include "nsBox.h"
#include "nsBoxLayoutState.h"
#include "nsGridLayout2.h"
#include "nsGridRow.h"
#include "mozilla/ReflowInput.h"

already_AddRefed<nsBoxLayout> NS_NewGridRowGroupLayout()
{
  RefPtr<nsBoxLayout> layout = new nsGridRowGroupLayout();
  return layout.forget();
} 

nsGridRowGroupLayout::nsGridRowGroupLayout():nsGridRowLayout(), mRowCount(0)
{
}

nsGridRowGroupLayout::~nsGridRowGroupLayout()
{
}

void
nsGridRowGroupLayout::ChildAddedOrRemoved(nsIFrame* aBox, nsBoxLayoutState& aState)
{
  int32_t index = 0;
  nsGrid* grid = GetGrid(aBox, &index);
  bool isHorizontal = IsXULHorizontal(aBox);

  if (grid)
    grid->RowAddedOrRemoved(aState, index, isHorizontal);
}

void
nsGridRowGroupLayout::AddWidth(nsSize& aSize, nscoord aSize2, bool aIsHorizontal)
{
  nscoord& size = GET_WIDTH(aSize, aIsHorizontal);

  if (size == NS_INTRINSICSIZE || aSize2 == NS_INTRINSICSIZE)
    size = NS_INTRINSICSIZE;
  else
    size += aSize2;
}

nsSize
nsGridRowGroupLayout::GetXULPrefSize(nsIFrame* aBox, nsBoxLayoutState& aState)
{ 
  nsSize vpref = nsGridRowLayout::GetXULPrefSize(aBox, aState); 


 /* It is possible that we could have some extra columns. This is when less columns in XUL were 
  * defined that needed. And example might be a grid with 3 defined columns but a row with 4 cells in 
  * it. We would need an extra column to make the grid work. But because that extra column does not 
  * have a box associated with it we must add its size in manually. Remember we could have extra rows
  * as well.
  */

  int32_t index = 0;
  nsGrid* grid = GetGrid(aBox, &index);

  if (grid) 
  {
    // make sure we add in extra columns sizes as well
    bool isHorizontal = IsXULHorizontal(aBox);
    int32_t extraColumns = grid->GetExtraColumnCount(isHorizontal);
    int32_t start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
    for (int32_t i=0; i < extraColumns; i++)
    {
      nscoord pref =
        grid->GetPrefRowHeight(aState, i+start, !isHorizontal); // GetPrefColumnWidth

      AddWidth(vpref, pref, isHorizontal);
    }
  }

  return vpref;
}

nsSize
nsGridRowGroupLayout::GetXULMaxSize(nsIFrame* aBox, nsBoxLayoutState& aState)
{
 nsSize maxSize = nsGridRowLayout::GetXULMaxSize(aBox, aState); 

  int32_t index = 0;
  nsGrid* grid = GetGrid(aBox, &index);

  if (grid) 
  {
    // make sure we add in extra columns sizes as well
    bool isHorizontal = IsXULHorizontal(aBox);
    int32_t extraColumns = grid->GetExtraColumnCount(isHorizontal);
    int32_t start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
    for (int32_t i=0; i < extraColumns; i++)
    {
      nscoord max =
        grid->GetMaxRowHeight(aState, i+start, !isHorizontal); // GetMaxColumnWidth

      AddWidth(maxSize, max, isHorizontal);
    }
  }

  return maxSize;
}

nsSize
nsGridRowGroupLayout::GetXULMinSize(nsIFrame* aBox, nsBoxLayoutState& aState)
{
  nsSize minSize = nsGridRowLayout::GetXULMinSize(aBox, aState); 

  int32_t index = 0;
  nsGrid* grid = GetGrid(aBox, &index);

  if (grid) 
  {
    // make sure we add in extra columns sizes as well
    bool isHorizontal = IsXULHorizontal(aBox);
    int32_t extraColumns = grid->GetExtraColumnCount(isHorizontal);
    int32_t start = grid->GetColumnCount(isHorizontal) - grid->GetExtraColumnCount(isHorizontal);
    for (int32_t i=0; i < extraColumns; i++)
    {
      nscoord min = 
        grid->GetMinRowHeight(aState, i+start, !isHorizontal); // GetMinColumnWidth
      AddWidth(minSize, min, isHorizontal);
    }
  }

  return minSize;
}

/*
 * Run down through our children dirtying them recursively.
 */
void
nsGridRowGroupLayout::DirtyRows(nsIFrame* aBox, nsBoxLayoutState& aState)
{
  if (aBox) {
    // mark us dirty
    // XXXldb We probably don't want to walk up the ancestor chain
    // calling MarkIntrinsicISizesDirty for every row group.
    aState.PresShell()->FrameNeedsReflow(aBox, nsIPresShell::eTreeChange,
                                         NS_FRAME_IS_DIRTY);
    nsIFrame* child = nsBox::GetChildXULBox(aBox);

    while(child) {

      // walk into scrollframes
      nsIFrame* deepChild = nsGrid::GetScrolledBox(child);

      // walk into other monuments
      nsIGridPart* monument = nsGrid::GetPartFromBox(deepChild);
      if (monument) 
        monument->DirtyRows(deepChild, aState);

      child = nsBox::GetNextXULBox(child);
    }
  }
}


void
nsGridRowGroupLayout::CountRowsColumns(nsIFrame* aBox, int32_t& aRowCount, int32_t& aComputedColumnCount)
{
  if (aBox) {
    int32_t startCount = aRowCount;

    nsIFrame* child = nsBox::GetChildXULBox(aBox);

    while(child) {
      
      // first see if it is a scrollframe. If so walk down into it and get the scrolled child
      nsIFrame* deepChild = nsGrid::GetScrolledBox(child);

      nsIGridPart* monument = nsGrid::GetPartFromBox(deepChild);
      if (monument) {
        monument->CountRowsColumns(deepChild, aRowCount, aComputedColumnCount);
        child = nsBox::GetNextXULBox(child);
        deepChild = child;
        continue;
      }

      child = nsBox::GetNextXULBox(child);

      // if not a monument. Then count it. It will be a bogus row
      aRowCount++;
    }

    mRowCount = aRowCount - startCount;
  }
}


/**
 * Fill out the given row structure recursively
 */
int32_t 
nsGridRowGroupLayout::BuildRows(nsIFrame* aBox, nsGridRow* aRows)
{ 
  int32_t rowCount = 0;

  if (aBox) {
    nsIFrame* child = nsBox::GetChildXULBox(aBox);

    while(child) {
      
      // first see if it is a scrollframe. If so walk down into it and get the scrolled child
      nsIFrame* deepChild = nsGrid::GetScrolledBox(child);

      nsIGridPart* monument = nsGrid::GetPartFromBox(deepChild);
      if (monument) {
        rowCount += monument->BuildRows(deepChild, &aRows[rowCount]);
        child = nsBox::GetNextXULBox(child);
        deepChild = child;
        continue;
      }

      aRows[rowCount].Init(child, true);

      child = nsBox::GetNextXULBox(child);

      // if not a monument. Then count it. It will be a bogus row
      rowCount++;
    }
  }

  return rowCount;
}

nsMargin
nsGridRowGroupLayout::GetTotalMargin(nsIFrame* aBox, bool aIsHorizontal)
{
  // group have border and padding added to the total margin

  nsMargin margin = nsGridRowLayout::GetTotalMargin(aBox, aIsHorizontal);
  
  // make sure we have the scrollframe on the outside if it has one.
  // that's where the border is.
  aBox = nsGrid::GetScrollBox(aBox);

  // add our border/padding to it
  nsMargin borderPadding(0,0,0,0);
  aBox->GetXULBorderAndPadding(borderPadding);
  margin += borderPadding;

  return margin;
}