Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; 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 :
6 : //
7 : // Eric Vaughan
8 : // Netscape Communications
9 : //
10 : // See documentation in associated header file
11 : //
12 :
13 : #include "nsGridLayout2.h"
14 : #include "nsGridRowGroupLayout.h"
15 : #include "nsGridRow.h"
16 : #include "nsBox.h"
17 : #include "nsIScrollableFrame.h"
18 : #include "nsSprocketLayout.h"
19 : #include "mozilla/ReflowInput.h"
20 :
21 : nsresult
22 0 : NS_NewGridLayout2( nsIPresShell* aPresShell, nsBoxLayout** aNewLayout)
23 : {
24 0 : *aNewLayout = new nsGridLayout2(aPresShell);
25 0 : NS_IF_ADDREF(*aNewLayout);
26 :
27 0 : return NS_OK;
28 :
29 : }
30 :
31 0 : nsGridLayout2::nsGridLayout2(nsIPresShell* aPresShell):nsStackLayout()
32 : {
33 0 : }
34 :
35 0 : nsGridLayout2::~nsGridLayout2()
36 : {
37 0 : }
38 :
39 : // static
40 : void
41 0 : nsGridLayout2::AddOffset(nsIFrame* aChild, nsSize& aSize)
42 : {
43 0 : nsMargin offset;
44 0 : GetOffset(aChild, offset);
45 0 : aSize.width += offset.left;
46 0 : aSize.height += offset.top;
47 0 : }
48 :
49 : NS_IMETHODIMP
50 0 : nsGridLayout2::XULLayout(nsIFrame* aBox, nsBoxLayoutState& aBoxLayoutState)
51 : {
52 : // XXX This should be set a better way!
53 0 : mGrid.SetBox(aBox);
54 0 : NS_ASSERTION(aBox->GetXULLayoutManager() == this, "setting incorrect box");
55 :
56 0 : nsresult rv = nsStackLayout::XULLayout(aBox, aBoxLayoutState);
57 : #ifdef DEBUG_grid
58 : mGrid.PrintCellMap();
59 : #endif
60 0 : return rv;
61 : }
62 :
63 : void
64 0 : nsGridLayout2::IntrinsicISizesDirty(nsIFrame* aBox, nsBoxLayoutState& aBoxLayoutState)
65 : {
66 0 : nsStackLayout::IntrinsicISizesDirty(aBox, aBoxLayoutState);
67 : // XXXldb We really don't need to do all the work that NeedsRebuild
68 : // does; we just need to mark intrinsic widths dirty on the
69 : // (row/column)(s/-groups).
70 0 : mGrid.NeedsRebuild(aBoxLayoutState);
71 0 : }
72 :
73 : nsGrid*
74 0 : nsGridLayout2::GetGrid(nsIFrame* aBox, int32_t* aIndex, nsGridRowLayout* aRequestor)
75 : {
76 : // XXX This should be set a better way!
77 0 : mGrid.SetBox(aBox);
78 0 : NS_ASSERTION(aBox->GetXULLayoutManager() == this, "setting incorrect box");
79 0 : return &mGrid;
80 : }
81 :
82 : void
83 0 : nsGridLayout2::AddWidth(nsSize& aSize, nscoord aSize2, bool aIsHorizontal)
84 : {
85 0 : nscoord& size = GET_WIDTH(aSize, aIsHorizontal);
86 :
87 0 : if (size != NS_INTRINSICSIZE) {
88 0 : if (aSize2 == NS_INTRINSICSIZE)
89 0 : size = NS_INTRINSICSIZE;
90 : else
91 0 : size += aSize2;
92 : }
93 0 : }
94 :
95 : nsSize
96 0 : nsGridLayout2::GetXULMinSize(nsIFrame* aBox, nsBoxLayoutState& aState)
97 : {
98 0 : nsSize minSize = nsStackLayout::GetXULMinSize(aBox, aState);
99 :
100 : // if there are no <rows> tags that will sum up our columns,
101 : // sum up our columns here.
102 0 : nsSize total(0,0);
103 0 : nsIFrame* rowsBox = mGrid.GetRowsBox();
104 0 : nsIFrame* columnsBox = mGrid.GetColumnsBox();
105 0 : if (!rowsBox || !columnsBox) {
106 0 : if (!rowsBox) {
107 : // max height is the sum of our rows
108 0 : int32_t rows = mGrid.GetRowCount();
109 0 : for (int32_t i=0; i < rows; i++)
110 : {
111 0 : nscoord height = mGrid.GetMinRowHeight(aState, i, true);
112 0 : AddWidth(total, height, false); // AddHeight
113 : }
114 : }
115 :
116 0 : if (!columnsBox) {
117 : // max height is the sum of our rows
118 0 : int32_t columns = mGrid.GetColumnCount();
119 0 : for (int32_t i=0; i < columns; i++)
120 : {
121 0 : nscoord width = mGrid.GetMinRowHeight(aState, i, false);
122 0 : AddWidth(total, width, true); // AddWidth
123 : }
124 : }
125 :
126 0 : AddMargin(aBox, total);
127 0 : AddOffset(aBox, total);
128 0 : AddLargestSize(minSize, total);
129 : }
130 :
131 0 : return minSize;
132 : }
133 :
134 : nsSize
135 0 : nsGridLayout2::GetXULPrefSize(nsIFrame* aBox, nsBoxLayoutState& aState)
136 : {
137 0 : nsSize pref = nsStackLayout::GetXULPrefSize(aBox, aState);
138 :
139 : // if there are no <rows> tags that will sum up our columns,
140 : // sum up our columns here.
141 0 : nsSize total(0,0);
142 0 : nsIFrame* rowsBox = mGrid.GetRowsBox();
143 0 : nsIFrame* columnsBox = mGrid.GetColumnsBox();
144 0 : if (!rowsBox || !columnsBox) {
145 0 : if (!rowsBox) {
146 : // max height is the sum of our rows
147 0 : int32_t rows = mGrid.GetRowCount();
148 0 : for (int32_t i=0; i < rows; i++)
149 : {
150 0 : nscoord height = mGrid.GetPrefRowHeight(aState, i, true);
151 0 : AddWidth(total, height, false); // AddHeight
152 : }
153 : }
154 :
155 0 : if (!columnsBox) {
156 : // max height is the sum of our rows
157 0 : int32_t columns = mGrid.GetColumnCount();
158 0 : for (int32_t i=0; i < columns; i++)
159 : {
160 0 : nscoord width = mGrid.GetPrefRowHeight(aState, i, false);
161 0 : AddWidth(total, width, true); // AddWidth
162 : }
163 : }
164 :
165 0 : AddMargin(aBox, total);
166 0 : AddOffset(aBox, total);
167 0 : AddLargestSize(pref, total);
168 : }
169 :
170 0 : return pref;
171 : }
172 :
173 : nsSize
174 0 : nsGridLayout2::GetXULMaxSize(nsIFrame* aBox, nsBoxLayoutState& aState)
175 : {
176 0 : nsSize maxSize = nsStackLayout::GetXULMaxSize(aBox, aState);
177 :
178 : // if there are no <rows> tags that will sum up our columns,
179 : // sum up our columns here.
180 0 : nsSize total(NS_INTRINSICSIZE, NS_INTRINSICSIZE);
181 0 : nsIFrame* rowsBox = mGrid.GetRowsBox();
182 0 : nsIFrame* columnsBox = mGrid.GetColumnsBox();
183 0 : if (!rowsBox || !columnsBox) {
184 0 : if (!rowsBox) {
185 0 : total.height = 0;
186 : // max height is the sum of our rows
187 0 : int32_t rows = mGrid.GetRowCount();
188 0 : for (int32_t i=0; i < rows; i++)
189 : {
190 0 : nscoord height = mGrid.GetMaxRowHeight(aState, i, true);
191 0 : AddWidth(total, height, false); // AddHeight
192 : }
193 : }
194 :
195 0 : if (!columnsBox) {
196 0 : total.width = 0;
197 : // max height is the sum of our rows
198 0 : int32_t columns = mGrid.GetColumnCount();
199 0 : for (int32_t i=0; i < columns; i++)
200 : {
201 0 : nscoord width = mGrid.GetMaxRowHeight(aState, i, false);
202 0 : AddWidth(total, width, true); // AddWidth
203 : }
204 : }
205 :
206 0 : AddMargin(aBox, total);
207 0 : AddOffset(aBox, total);
208 0 : AddSmallestSize(maxSize, total);
209 : }
210 :
211 0 : return maxSize;
212 : }
213 :
214 : int32_t
215 0 : nsGridLayout2::BuildRows(nsIFrame* aBox, nsGridRow* aRows)
216 : {
217 0 : if (aBox) {
218 0 : aRows[0].Init(aBox, true);
219 0 : return 1;
220 : }
221 0 : return 0;
222 : }
223 :
224 : nsMargin
225 0 : nsGridLayout2::GetTotalMargin(nsIFrame* aBox, bool aIsHorizontal)
226 : {
227 0 : nsMargin margin(0,0,0,0);
228 0 : return margin;
229 : }
230 :
231 : void
232 0 : nsGridLayout2::ChildrenInserted(nsIFrame* aBox, nsBoxLayoutState& aState,
233 : nsIFrame* aPrevBox,
234 : const nsFrameList::Slice& aNewChildren)
235 : {
236 0 : mGrid.NeedsRebuild(aState);
237 0 : }
238 :
239 : void
240 0 : nsGridLayout2::ChildrenAppended(nsIFrame* aBox, nsBoxLayoutState& aState,
241 : const nsFrameList::Slice& aNewChildren)
242 : {
243 0 : mGrid.NeedsRebuild(aState);
244 0 : }
245 :
246 : void
247 0 : nsGridLayout2::ChildrenRemoved(nsIFrame* aBox, nsBoxLayoutState& aState,
248 : nsIFrame* aChildList)
249 : {
250 0 : mGrid.NeedsRebuild(aState);
251 0 : }
252 :
253 : void
254 0 : nsGridLayout2::ChildrenSet(nsIFrame* aBox, nsBoxLayoutState& aState,
255 : nsIFrame* aChildList)
256 : {
257 0 : mGrid.NeedsRebuild(aState);
258 0 : }
259 :
260 0 : NS_IMPL_ADDREF_INHERITED(nsGridLayout2, nsStackLayout)
261 0 : NS_IMPL_RELEASE_INHERITED(nsGridLayout2, nsStackLayout)
262 :
263 0 : NS_INTERFACE_MAP_BEGIN(nsGridLayout2)
264 0 : NS_INTERFACE_MAP_ENTRY(nsIGridPart)
265 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGridPart)
266 0 : NS_INTERFACE_MAP_END_INHERITING(nsStackLayout)
|