|
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 "nsGridRowLayout.h" |
|
14 #include "nsBoxLayoutState.h" |
|
15 #include "nsIScrollableFrame.h" |
|
16 #include "nsBox.h" |
|
17 #include "nsStackLayout.h" |
|
18 #include "nsGrid.h" |
|
19 |
|
20 nsGridRowLayout::nsGridRowLayout():nsSprocketLayout() |
|
21 { |
|
22 } |
|
23 |
|
24 void |
|
25 nsGridRowLayout::ChildrenInserted(nsIFrame* aBox, nsBoxLayoutState& aState, |
|
26 nsIFrame* aPrevBox, |
|
27 const nsFrameList::Slice& aNewChildren) |
|
28 { |
|
29 ChildAddedOrRemoved(aBox, aState); |
|
30 } |
|
31 |
|
32 void |
|
33 nsGridRowLayout::ChildrenAppended(nsIFrame* aBox, nsBoxLayoutState& aState, |
|
34 const nsFrameList::Slice& aNewChildren) |
|
35 { |
|
36 ChildAddedOrRemoved(aBox, aState); |
|
37 } |
|
38 |
|
39 void |
|
40 nsGridRowLayout::ChildrenRemoved(nsIFrame* aBox, nsBoxLayoutState& aState, nsIFrame* aChildList) |
|
41 { |
|
42 ChildAddedOrRemoved(aBox, aState); |
|
43 } |
|
44 |
|
45 void |
|
46 nsGridRowLayout::ChildrenSet(nsIFrame* aBox, nsBoxLayoutState& aState, nsIFrame* aChildList) |
|
47 { |
|
48 ChildAddedOrRemoved(aBox, aState); |
|
49 } |
|
50 |
|
51 nsIGridPart* |
|
52 nsGridRowLayout::GetParentGridPart(nsIFrame* aBox, nsIFrame** aParentBox) |
|
53 { |
|
54 // go up and find our parent gridRow. Skip and non gridRow |
|
55 // parents. |
|
56 *aParentBox = nullptr; |
|
57 |
|
58 // walk up through any scrollboxes |
|
59 aBox = nsGrid::GetScrollBox(aBox); |
|
60 |
|
61 // get the parent |
|
62 if (aBox) |
|
63 aBox = aBox->GetParentBox(); |
|
64 |
|
65 if (aBox) |
|
66 { |
|
67 nsIGridPart* parentGridRow = nsGrid::GetPartFromBox(aBox); |
|
68 if (parentGridRow && parentGridRow->CanContain(this)) { |
|
69 *aParentBox = aBox; |
|
70 return parentGridRow; |
|
71 } |
|
72 } |
|
73 |
|
74 return nullptr; |
|
75 } |
|
76 |
|
77 |
|
78 nsGrid* |
|
79 nsGridRowLayout::GetGrid(nsIFrame* aBox, int32_t* aIndex, nsGridRowLayout* aRequestor) |
|
80 { |
|
81 |
|
82 if (aRequestor == nullptr) |
|
83 { |
|
84 nsIFrame* parentBox; // nsIFrame is implemented by nsIFrame and is not refcounted. |
|
85 nsIGridPart* parent = GetParentGridPart(aBox, &parentBox); |
|
86 if (parent) |
|
87 return parent->GetGrid(parentBox, aIndex, this); |
|
88 return nullptr; |
|
89 } |
|
90 |
|
91 int32_t index = -1; |
|
92 nsIFrame* child = aBox->GetChildBox(); |
|
93 int32_t count = 0; |
|
94 while(child) |
|
95 { |
|
96 // if there is a scrollframe walk inside it to its child |
|
97 nsIFrame* childBox = nsGrid::GetScrolledBox(child); |
|
98 |
|
99 nsBoxLayout* layout = childBox->GetLayoutManager(); |
|
100 nsIGridPart* gridRow = nsGrid::GetPartFromBox(childBox); |
|
101 if (gridRow) |
|
102 { |
|
103 if (layout == aRequestor) { |
|
104 index = count; |
|
105 break; |
|
106 } |
|
107 count += gridRow->GetRowCount(); |
|
108 } else |
|
109 count++; |
|
110 |
|
111 child = child->GetNextBox(); |
|
112 } |
|
113 |
|
114 // if we didn't find ourselves then the tree isn't properly formed yet |
|
115 // this could happen during initial construction so lets just |
|
116 // fail. |
|
117 if (index == -1) { |
|
118 *aIndex = -1; |
|
119 return nullptr; |
|
120 } |
|
121 |
|
122 (*aIndex) += index; |
|
123 |
|
124 nsIFrame* parentBox; // nsIFrame is implemented by nsIFrame and is not refcounted. |
|
125 nsIGridPart* parent = GetParentGridPart(aBox, &parentBox); |
|
126 if (parent) |
|
127 return parent->GetGrid(parentBox, aIndex, this); |
|
128 |
|
129 return nullptr; |
|
130 } |
|
131 |
|
132 nsMargin |
|
133 nsGridRowLayout::GetTotalMargin(nsIFrame* aBox, bool aIsHorizontal) |
|
134 { |
|
135 // get our parents margin |
|
136 nsMargin margin(0,0,0,0); |
|
137 nsIFrame* parent = nullptr; |
|
138 nsIGridPart* part = GetParentGridPart(aBox, &parent); |
|
139 if (part && parent) { |
|
140 // if we are the first or last child walk upward and add margins. |
|
141 |
|
142 // make sure we check for a scrollbox |
|
143 aBox = nsGrid::GetScrollBox(aBox); |
|
144 |
|
145 // see if we have a next to see if we are last |
|
146 nsIFrame* next = aBox->GetNextBox(); |
|
147 |
|
148 // get the parent first child to see if we are first |
|
149 nsIFrame* child = parent->GetChildBox(); |
|
150 |
|
151 margin = part->GetTotalMargin(parent, aIsHorizontal); |
|
152 |
|
153 // if first or last |
|
154 if (child == aBox || next == nullptr) { |
|
155 |
|
156 // if it's not the first child remove the top margin |
|
157 // we don't need it. |
|
158 if (child != aBox) |
|
159 { |
|
160 if (aIsHorizontal) |
|
161 margin.top = 0; |
|
162 else |
|
163 margin.left = 0; |
|
164 } |
|
165 |
|
166 // if it's not the last child remove the bottom margin |
|
167 // we don't need it. |
|
168 if (next != nullptr) |
|
169 { |
|
170 if (aIsHorizontal) |
|
171 margin.bottom = 0; |
|
172 else |
|
173 margin.right = 0; |
|
174 } |
|
175 |
|
176 } |
|
177 } |
|
178 |
|
179 // add ours to it. |
|
180 nsMargin ourMargin; |
|
181 aBox->GetMargin(ourMargin); |
|
182 margin += ourMargin; |
|
183 |
|
184 return margin; |
|
185 } |
|
186 |
|
187 NS_IMPL_ADDREF_INHERITED(nsGridRowLayout, nsBoxLayout) |
|
188 NS_IMPL_RELEASE_INHERITED(nsGridRowLayout, nsBoxLayout) |
|
189 |
|
190 NS_INTERFACE_MAP_BEGIN(nsGridRowLayout) |
|
191 NS_INTERFACE_MAP_ENTRY(nsIGridPart) |
|
192 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIGridPart) |
|
193 NS_INTERFACE_MAP_END_INHERITING(nsBoxLayout) |