Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / generic / nsSplittableFrame.cpp
blobcbf7f360038841a6fe7aab07e6d4c1fd01272fdb
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * base class for rendering objects that can be split across lines,
40 * columns, or pages
43 #include "nsSplittableFrame.h"
44 #include "nsIContent.h"
45 #include "nsPresContext.h"
46 #include "nsStyleContext.h"
48 NS_IMETHODIMP
49 nsSplittableFrame::Init(nsIContent* aContent,
50 nsIFrame* aParent,
51 nsIFrame* aPrevInFlow)
53 nsresult rv = nsFrame::Init(aContent, aParent, aPrevInFlow);
55 if (aPrevInFlow) {
56 // Hook the frame into the flow
57 SetPrevInFlow(aPrevInFlow);
58 aPrevInFlow->SetNextInFlow(this);
61 return rv;
64 void
65 nsSplittableFrame::Destroy()
67 // Disconnect from the flow list
68 if (mPrevContinuation || mNextContinuation) {
69 RemoveFromFlow(this);
72 // Let the base class destroy the frame
73 nsFrame::Destroy();
76 nsSplittableType
77 nsSplittableFrame::GetSplittableType() const
79 return NS_FRAME_SPLITTABLE;
82 nsIFrame* nsSplittableFrame::GetPrevContinuation() const
84 return mPrevContinuation;
87 NS_METHOD nsSplittableFrame::SetPrevContinuation(nsIFrame* aFrame)
89 NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a prev continuation with incorrect type!");
90 NS_ASSERTION (!IsInPrevContinuationChain(aFrame, this), "creating a loop in continuation chain!");
91 mPrevContinuation = aFrame;
92 RemoveStateBits(NS_FRAME_IS_FLUID_CONTINUATION);
93 return NS_OK;
96 nsIFrame* nsSplittableFrame::GetNextContinuation() const
98 return mNextContinuation;
101 NS_METHOD nsSplittableFrame::SetNextContinuation(nsIFrame* aFrame)
103 NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a next continuation with incorrect type!");
104 NS_ASSERTION (!IsInNextContinuationChain(aFrame, this), "creating a loop in continuation chain!");
105 mNextContinuation = aFrame;
106 if (aFrame)
107 aFrame->RemoveStateBits(NS_FRAME_IS_FLUID_CONTINUATION);
108 return NS_OK;
111 nsIFrame* nsSplittableFrame::GetFirstContinuation() const
113 nsSplittableFrame* firstContinuation = const_cast<nsSplittableFrame*>(this);
114 while (firstContinuation->mPrevContinuation) {
115 firstContinuation = static_cast<nsSplittableFrame*>(firstContinuation->mPrevContinuation);
117 NS_POSTCONDITION(firstContinuation, "illegal state in continuation chain.");
118 return firstContinuation;
121 nsIFrame* nsSplittableFrame::GetLastContinuation() const
123 nsSplittableFrame* lastContinuation = const_cast<nsSplittableFrame*>(this);
124 while (lastContinuation->mNextContinuation) {
125 lastContinuation = static_cast<nsSplittableFrame*>(lastContinuation->mNextContinuation);
127 NS_POSTCONDITION(lastContinuation, "illegal state in continuation chain.");
128 return lastContinuation;
131 #ifdef DEBUG
132 PRBool nsSplittableFrame::IsInPrevContinuationChain(nsIFrame* aFrame1, nsIFrame* aFrame2)
134 while (aFrame1) {
135 if (aFrame1 == aFrame2)
136 return PR_TRUE;
137 aFrame1 = aFrame1->GetPrevContinuation();
139 return PR_FALSE;
142 PRBool nsSplittableFrame::IsInNextContinuationChain(nsIFrame* aFrame1, nsIFrame* aFrame2)
144 while (aFrame1) {
145 if (aFrame1 == aFrame2)
146 return PR_TRUE;
147 aFrame1 = aFrame1->GetNextContinuation();
149 return PR_FALSE;
151 #endif
153 nsIFrame* nsSplittableFrame::GetPrevInFlow() const
155 return (GetStateBits() & NS_FRAME_IS_FLUID_CONTINUATION) ? mPrevContinuation : nsnull;
158 NS_METHOD nsSplittableFrame::SetPrevInFlow(nsIFrame* aFrame)
160 NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a prev in flow with incorrect type!");
161 NS_ASSERTION (!IsInPrevContinuationChain(aFrame, this), "creating a loop in continuation chain!");
162 mPrevContinuation = aFrame;
163 AddStateBits(NS_FRAME_IS_FLUID_CONTINUATION);
164 return NS_OK;
167 nsIFrame* nsSplittableFrame::GetNextInFlow() const
169 return mNextContinuation && (mNextContinuation->GetStateBits() & NS_FRAME_IS_FLUID_CONTINUATION) ?
170 mNextContinuation : nsnull;
173 NS_METHOD nsSplittableFrame::SetNextInFlow(nsIFrame* aFrame)
175 NS_ASSERTION (!aFrame || GetType() == aFrame->GetType(), "setting a next in flow with incorrect type!");
176 NS_ASSERTION (!IsInNextContinuationChain(aFrame, this), "creating a loop in continuation chain!");
177 mNextContinuation = aFrame;
178 if (aFrame)
179 aFrame->AddStateBits(NS_FRAME_IS_FLUID_CONTINUATION);
180 return NS_OK;
183 nsIFrame* nsSplittableFrame::GetFirstInFlow() const
185 nsSplittableFrame* firstInFlow = const_cast<nsSplittableFrame*>(this);
186 while (nsIFrame *prev = firstInFlow->GetPrevInFlow()) {
187 firstInFlow = static_cast<nsSplittableFrame*>(prev);
189 NS_POSTCONDITION(firstInFlow, "illegal state in flow chain.");
190 return firstInFlow;
193 nsIFrame* nsSplittableFrame::GetLastInFlow() const
195 nsSplittableFrame* lastInFlow = const_cast<nsSplittableFrame*>(this);
196 while (nsIFrame* next = lastInFlow->GetNextInFlow()) {
197 lastInFlow = static_cast<nsSplittableFrame*>(next);
199 NS_POSTCONDITION(lastInFlow, "illegal state in flow chain.");
200 return lastInFlow;
203 // Remove this frame from the flow. Connects prev in flow and next in flow
204 void
205 nsSplittableFrame::RemoveFromFlow(nsIFrame* aFrame)
207 nsIFrame* prevContinuation = aFrame->GetPrevContinuation();
208 nsIFrame* nextContinuation = aFrame->GetNextContinuation();
210 // The new continuation is fluid only if the continuation on both sides
211 // of the removed frame was fluid
212 if (aFrame->GetPrevInFlow() && aFrame->GetNextInFlow()) {
213 if (prevContinuation) {
214 prevContinuation->SetNextInFlow(nextContinuation);
216 if (nextContinuation) {
217 nextContinuation->SetPrevInFlow(prevContinuation);
219 } else {
220 if (prevContinuation) {
221 prevContinuation->SetNextContinuation(nextContinuation);
223 if (nextContinuation) {
224 nextContinuation->SetPrevContinuation(prevContinuation);
228 aFrame->SetPrevInFlow(nsnull);
229 aFrame->SetNextInFlow(nsnull);
232 // Detach from previous frame in flow
233 void
234 nsSplittableFrame::BreakFromPrevFlow(nsIFrame* aFrame)
236 nsIFrame* prevInFlow = aFrame->GetPrevInFlow();
237 // If this frame has a non-fluid continuation, transfer it to its prevInFlow
238 nsIFrame* nextNonFluid = nsnull;
239 nsIFrame* nextContinuation = aFrame->GetNextContinuation();
240 if (nextContinuation && !(nextContinuation->GetStateBits() & NS_FRAME_IS_FLUID_CONTINUATION)) {
241 nextNonFluid = nextContinuation;
242 aFrame->SetNextContinuation(nsnull);
244 if (prevInFlow) {
245 if (nextNonFluid) {
246 prevInFlow->SetNextContinuation(nextNonFluid);
247 nextNonFluid->SetPrevContinuation(prevInFlow);
248 } else {
249 prevInFlow->SetNextInFlow(nsnull);
251 aFrame->SetPrevInFlow(nsnull);
255 #ifdef DEBUG
256 void
257 nsSplittableFrame::DumpBaseRegressionData(nsPresContext* aPresContext, FILE* out, PRInt32 aIndent, PRBool aIncludeStyleData)
259 nsFrame::DumpBaseRegressionData(aPresContext, out, aIndent, aIncludeStyleData);
260 if (nsnull != mNextContinuation) {
261 IndentBy(out, aIndent);
262 fprintf(out, "<next-continuation va=\"%ld\"/>\n", PRUptrdiff(mNextContinuation));
264 if (nsnull != mPrevContinuation) {
265 IndentBy(out, aIndent);
266 fprintf(out, "<prev-continuation va=\"%ld\"/>\n", PRUptrdiff(mPrevContinuation));
270 #endif