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
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.
23 * Michael Ventnor <m.ventnor@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 /* base class #2 for rendering objects that have child lists */
41 #include "nsHTMLContainerFrame.h"
42 #include "nsFirstLetterFrame.h"
43 #include "nsIRenderingContext.h"
44 #include "nsPresContext.h"
45 #include "nsIPresShell.h"
46 #include "nsStyleContext.h"
47 #include "nsStyleConsts.h"
48 #include "nsIContent.h"
49 #include "nsGkAtoms.h"
50 #include "nsLayoutUtils.h"
51 #include "nsCSSAnonBoxes.h"
52 #include "nsILinkHandler.h"
53 #include "nsGUIEvent.h"
54 #include "nsIDocument.h"
56 #include "nsPlaceholderFrame.h"
57 #include "nsHTMLParts.h"
59 #include "nsIViewManager.h"
60 #include "nsIDOMEvent.h"
61 #include "nsWidgetsCID.h"
63 #include "nsIDeviceContext.h"
64 #include "nsIFontMetrics.h"
65 #include "nsIThebesFontMetrics.h"
67 #include "nsCSSFrameConstructor.h"
68 #include "nsDisplayList.h"
69 #include "nsBlockFrame.h"
70 #include "nsLineBox.h"
71 #include "nsDisplayList.h"
72 #include "nsCSSRendering.h"
74 class nsDisplayTextDecoration
: public nsDisplayItem
{
76 nsDisplayTextDecoration(nsDisplayListBuilder
* aBuilder
,
77 nsHTMLContainerFrame
* aFrame
, PRUint8 aDecoration
,
78 nscolor aColor
, nsLineBox
* aLine
)
79 : nsDisplayItem(aBuilder
, aFrame
), mLine(aLine
), mColor(aColor
),
80 mDecoration(aDecoration
) {
81 MOZ_COUNT_CTOR(nsDisplayTextDecoration
);
83 #ifdef NS_BUILD_REFCNT_LOGGING
84 virtual ~nsDisplayTextDecoration() {
85 MOZ_COUNT_DTOR(nsDisplayTextDecoration
);
89 virtual void Paint(nsDisplayListBuilder
* aBuilder
,
90 nsIRenderingContext
* aCtx
);
91 virtual nsRect
GetBounds(nsDisplayListBuilder
* aBuilder
);
92 NS_DISPLAY_DECL_NAME("TextDecoration", TYPE_TEXT_DECORATION
)
94 virtual PRUint32
GetPerFrameKey()
96 return TYPE_TEXT_DECORATION
| (mDecoration
<< TYPE_BITS
);
106 nsDisplayTextDecoration::Paint(nsDisplayListBuilder
* aBuilder
,
107 nsIRenderingContext
* aCtx
)
109 nsCOMPtr
<nsIFontMetrics
> fm
;
110 nsLayoutUtils::GetFontMetricsForFrame(mFrame
, getter_AddRefs(fm
));
111 nsIThebesFontMetrics
* tfm
= static_cast<nsIThebesFontMetrics
*>(fm
.get());
112 gfxFontGroup
* fontGroup
= tfm
->GetThebesFontGroup();
113 gfxFont
* firstFont
= fontGroup
->GetFontAt(0);
116 const gfxFont::Metrics
& metrics
= firstFont
->GetMetrics();
119 // The ascent of first-letter frame's text may not be the same as the ascent
120 // of the font metrics. Because that may use the tight box of the actual
122 if (mFrame
->GetType() == nsGkAtoms::letterFrame
) {
123 // Note that nsFirstLetterFrame::GetFirstLetterBaseline() returns
124 // |border-top + padding-top + ascent|. But we only need the ascent value.
125 // Because they will be added in PaintTextDecorationLine.
126 nsFirstLetterFrame
* letterFrame
= static_cast<nsFirstLetterFrame
*>(mFrame
);
127 nscoord tmp
= letterFrame
->GetFirstLetterBaseline();
128 tmp
-= letterFrame
->GetUsedBorderAndPadding().top
;
129 ascent
= letterFrame
->PresContext()->AppUnitsToGfxUnits(tmp
);
131 ascent
= metrics
.maxAscent
;
134 nsPoint pt
= ToReferenceFrame();
135 nsHTMLContainerFrame
* f
= static_cast<nsHTMLContainerFrame
*>(mFrame
);
136 if (mDecoration
== NS_STYLE_TEXT_DECORATION_UNDERLINE
) {
137 gfxFloat underlineOffset
= fontGroup
->GetUnderlineOffset();
138 f
->PaintTextDecorationLine(aCtx
->ThebesContext(), pt
, mLine
, mColor
,
139 underlineOffset
, ascent
,
140 metrics
.underlineSize
, mDecoration
);
141 } else if (mDecoration
== NS_STYLE_TEXT_DECORATION_OVERLINE
) {
142 f
->PaintTextDecorationLine(aCtx
->ThebesContext(), pt
, mLine
, mColor
,
143 metrics
.maxAscent
, ascent
,
144 metrics
.underlineSize
, mDecoration
);
146 f
->PaintTextDecorationLine(aCtx
->ThebesContext(), pt
, mLine
, mColor
,
147 metrics
.strikeoutOffset
, ascent
,
148 metrics
.strikeoutSize
, mDecoration
);
153 nsDisplayTextDecoration::GetBounds(nsDisplayListBuilder
* aBuilder
)
155 return mFrame
->GetOverflowRect() + ToReferenceFrame();
158 class nsDisplayTextShadow
: public nsDisplayItem
{
160 nsDisplayTextShadow(nsDisplayListBuilder
* aBuilder
,
161 nsHTMLContainerFrame
* aFrame
,
162 const PRUint8 aDecoration
,
164 : nsDisplayItem(aBuilder
, aFrame
), mLine(aLine
),
165 mDecorationFlags(aDecoration
) {
166 MOZ_COUNT_CTOR(nsDisplayTextShadow
);
168 virtual ~nsDisplayTextShadow() {
169 MOZ_COUNT_DTOR(nsDisplayTextShadow
);
172 virtual void Paint(nsDisplayListBuilder
* aBuilder
,
173 nsIRenderingContext
* aCtx
);
174 virtual nsRect
GetBounds(nsDisplayListBuilder
* aBuilder
);
175 NS_DISPLAY_DECL_NAME("TextShadowContainer", TYPE_TEXT_SHADOW
)
178 PRUint8 mDecorationFlags
;
182 nsDisplayTextShadow::Paint(nsDisplayListBuilder
* aBuilder
,
183 nsIRenderingContext
* aCtx
)
185 nsCOMPtr
<nsIFontMetrics
> fm
;
186 nsLayoutUtils::GetFontMetricsForFrame(mFrame
, getter_AddRefs(fm
));
187 nsIThebesFontMetrics
* tfm
= static_cast<nsIThebesFontMetrics
*>(fm
.get());
188 gfxFontGroup
* fontGroup
= tfm
->GetThebesFontGroup();
189 gfxFont
* firstFont
= fontGroup
->GetFontAt(0);
193 const gfxFont::Metrics
& metrics
= firstFont
->GetMetrics();
194 gfxFloat underlineOffset
= fontGroup
->GetUnderlineOffset();
196 nsHTMLContainerFrame
* f
= static_cast<nsHTMLContainerFrame
*>(mFrame
);
197 nsPresContext
* presContext
= mFrame
->PresContext();
198 gfxContext
* thebesCtx
= aCtx
->ThebesContext();
204 // Block frames give us an nsLineBox, so we must use that
205 nscoord width
= mLine
->mBounds
.width
;
206 start
= mLine
->mBounds
.x
;
207 f
->AdjustForTextIndent(mLine
, start
, width
);
211 lineWidth
= presContext
->AppUnitsToGfxUnits(width
);
212 ascent
= presContext
->AppUnitsToGfxUnits(mLine
->GetAscent());
214 // For inline frames, we must use the frame's geometry
215 lineWidth
= presContext
->AppUnitsToGfxUnits(mFrame
->GetContentRect().width
);
217 // The ascent of :first-letter frame's text may not be the same as the ascent
218 // of the font metrics, because it may use the tight box of the actual
220 if (mFrame
->GetType() == nsGkAtoms::letterFrame
) {
221 // Note that nsFirstLetterFrame::GetFirstLetterBaseline() returns
222 // |border-top + padding-top + ascent|. But we only need the ascent value,
223 // because those will be added in PaintTextDecorationLine.
224 nsFirstLetterFrame
* letterFrame
= static_cast<nsFirstLetterFrame
*>(mFrame
);
225 nscoord tmp
= letterFrame
->GetFirstLetterBaseline();
226 tmp
-= letterFrame
->GetUsedBorderAndPadding().top
;
227 ascent
= presContext
->AppUnitsToGfxUnits(tmp
);
229 ascent
= metrics
.maxAscent
;
233 nsCSSShadowArray
* shadowList
= mFrame
->GetStyleText()->mTextShadow
;
234 NS_ABORT_IF_FALSE(shadowList
,
235 "Why did we make a display list item if we have no shadows?");
237 // Get the rects for each text decoration line, so we know how big we
238 // can make each shadow's surface
239 nsRect underlineRect
;
241 nsRect lineThroughRect
;
242 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_UNDERLINE
) {
243 gfxSize
size(lineWidth
, metrics
.underlineSize
);
244 underlineRect
= nsCSSRendering::GetTextDecorationRect(presContext
, size
,
245 ascent
, underlineOffset
,
246 NS_STYLE_TEXT_DECORATION_UNDERLINE
,
247 nsCSSRendering::DECORATION_STYLE_SOLID
);
249 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_OVERLINE
) {
250 gfxSize
size(lineWidth
, metrics
.underlineSize
);
251 overlineRect
= nsCSSRendering::GetTextDecorationRect(presContext
, size
,
252 ascent
, metrics
.maxAscent
,
253 NS_STYLE_TEXT_DECORATION_OVERLINE
,
254 nsCSSRendering::DECORATION_STYLE_SOLID
);
256 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_LINE_THROUGH
) {
257 gfxSize
size(lineWidth
, metrics
.strikeoutSize
);
258 lineThroughRect
= nsCSSRendering::GetTextDecorationRect(presContext
, size
,
259 ascent
, metrics
.strikeoutOffset
,
260 NS_STYLE_TEXT_DECORATION_LINE_THROUGH
,
261 nsCSSRendering::DECORATION_STYLE_SOLID
);
264 for (PRUint32 i
= shadowList
->Length(); i
> 0; --i
) {
265 nsCSSShadowItem
* shadow
= shadowList
->ShadowAt(i
- 1);
267 nscolor shadowColor
=
268 shadow
->mHasColor
? shadow
->mColor
: mFrame
->GetStyleColor()->mColor
;
270 nsPoint pt
= ToReferenceFrame() +
271 nsPoint(shadow
->mXOffset
, shadow
->mYOffset
);
274 linePt
= nsPoint(start
+ pt
.x
, mLine
->mBounds
.y
+ pt
.y
);
276 linePt
= mFrame
->GetContentRect().TopLeft() - mFrame
->GetPosition() + pt
;
279 nsRect
shadowRect(0, 0, 0, 0);
280 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_UNDERLINE
) {
281 shadowRect
.UnionRect(shadowRect
, underlineRect
+ linePt
);
283 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_OVERLINE
) {
284 shadowRect
.UnionRect(shadowRect
, overlineRect
+ linePt
);
286 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_LINE_THROUGH
) {
287 shadowRect
.UnionRect(shadowRect
, lineThroughRect
+ linePt
);
290 gfxContextAutoSaveRestore
save(thebesCtx
);
291 thebesCtx
->NewPath();
292 thebesCtx
->SetColor(gfxRGBA(shadowColor
));
294 // Create our shadow surface, then paint the text decorations onto it
295 nsContextBoxBlur contextBoxBlur
;
296 gfxContext
* shadowCtx
= contextBoxBlur
.Init(shadowRect
, 0, shadow
->mRadius
,
297 presContext
->AppUnitsPerDevPixel(),
298 thebesCtx
, mVisibleRect
, nsnull
);
303 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_UNDERLINE
) {
304 f
->PaintTextDecorationLine(shadowCtx
, pt
, mLine
, shadowColor
,
305 underlineOffset
, ascent
,
306 metrics
.underlineSize
, NS_STYLE_TEXT_DECORATION_UNDERLINE
);
308 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_OVERLINE
) {
309 f
->PaintTextDecorationLine(shadowCtx
, pt
, mLine
, shadowColor
,
310 metrics
.maxAscent
, ascent
,
311 metrics
.underlineSize
, NS_STYLE_TEXT_DECORATION_OVERLINE
);
313 if (mDecorationFlags
& NS_STYLE_TEXT_DECORATION_LINE_THROUGH
) {
314 f
->PaintTextDecorationLine(shadowCtx
, pt
, mLine
, shadowColor
,
315 metrics
.strikeoutOffset
, ascent
,
316 metrics
.strikeoutSize
, NS_STYLE_TEXT_DECORATION_LINE_THROUGH
);
319 contextBoxBlur
.DoPaint();
324 nsDisplayTextShadow::GetBounds(nsDisplayListBuilder
* aBuilder
)
326 // Shadows are always painted in the overflow rect
327 return mFrame
->GetOverflowRect() + ToReferenceFrame();
331 nsHTMLContainerFrame::DisplayTextDecorations(nsDisplayListBuilder
* aBuilder
,
332 nsDisplayList
* aBelowTextDecorations
,
333 nsDisplayList
* aAboveTextDecorations
,
336 if (eCompatibility_NavQuirks
== PresContext()->CompatibilityMode())
338 if (!IsVisibleForPainting(aBuilder
))
341 // Do standards mode painting of 'text-decoration's: under+overline
342 // behind children, line-through in front. For Quirks mode, see
343 // nsTextFrame::PaintTextDecorations. (See bug 1777.)
344 nscolor underColor
, overColor
, strikeColor
;
345 PRUint8 decorations
= NS_STYLE_TEXT_DECORATION_NONE
;
346 GetTextDecorations(PresContext(), aLine
!= nsnull
, decorations
, underColor
,
347 overColor
, strikeColor
);
349 if (decorations
== NS_STYLE_TEXT_DECORATION_NONE
)
352 // The text-shadow spec says that any text decorations must also have a
353 // shadow applied to them. So draw the shadows as part of the display
354 // list, underneath the text and all decorations.
355 if (GetStyleText()->mTextShadow
) {
356 nsresult rv
= aBelowTextDecorations
->AppendNewToTop(new (aBuilder
)
357 nsDisplayTextShadow(aBuilder
, this, decorations
, aLine
));
358 NS_ENSURE_SUCCESS(rv
, rv
);
361 if (decorations
& NS_STYLE_TEXT_DECORATION_UNDERLINE
) {
362 nsresult rv
= aBelowTextDecorations
->AppendNewToTop(new (aBuilder
)
363 nsDisplayTextDecoration(aBuilder
, this, NS_STYLE_TEXT_DECORATION_UNDERLINE
,
365 NS_ENSURE_SUCCESS(rv
, rv
);
367 if (decorations
& NS_STYLE_TEXT_DECORATION_OVERLINE
) {
368 nsresult rv
= aBelowTextDecorations
->AppendNewToTop(new (aBuilder
)
369 nsDisplayTextDecoration(aBuilder
, this, NS_STYLE_TEXT_DECORATION_OVERLINE
,
371 NS_ENSURE_SUCCESS(rv
, rv
);
373 if (decorations
& NS_STYLE_TEXT_DECORATION_LINE_THROUGH
) {
374 nsresult rv
= aAboveTextDecorations
->AppendNewToTop(new (aBuilder
)
375 nsDisplayTextDecoration(aBuilder
, this, NS_STYLE_TEXT_DECORATION_LINE_THROUGH
,
376 strikeColor
, aLine
));
377 NS_ENSURE_SUCCESS(rv
, rv
);
383 nsHTMLContainerFrame::DisplayTextDecorationsAndChildren(
384 nsDisplayListBuilder
* aBuilder
, const nsRect
& aDirtyRect
,
385 const nsDisplayListSet
& aLists
)
387 nsDisplayList aboveChildrenDecorations
;
388 nsresult rv
= DisplayTextDecorations(aBuilder
, aLists
.Content(),
389 &aboveChildrenDecorations
, nsnull
);
390 NS_ENSURE_SUCCESS(rv
, rv
);
392 rv
= BuildDisplayListForNonBlockChildren(aBuilder
, aDirtyRect
, aLists
,
393 DISPLAY_CHILD_INLINE
);
394 NS_ENSURE_SUCCESS(rv
, rv
);
396 aLists
.Content()->AppendToTop(&aboveChildrenDecorations
);
401 nsHTMLContainerFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
402 const nsRect
& aDirtyRect
,
403 const nsDisplayListSet
& aLists
) {
404 nsresult rv
= DisplayBorderBackgroundOutline(aBuilder
, aLists
);
405 NS_ENSURE_SUCCESS(rv
, rv
);
407 return DisplayTextDecorationsAndChildren(aBuilder
, aDirtyRect
, aLists
);
411 HasTextFrameDescendantOrInFlow(nsIFrame
* aFrame
);
414 nsHTMLContainerFrame::PaintTextDecorationLine(
422 const PRUint8 aDecoration
)
424 NS_ASSERTION(!aLine
, "Should not have passed a linebox to a non-block frame");
425 nsMargin bp
= GetUsedBorderAndPadding();
426 PRIntn skip
= GetSkipSides();
427 NS_FOR_CSS_SIDES(side
) {
428 if (skip
& (1 << side
)) {
432 nscoord innerWidth
= mRect
.width
- bp
.left
- bp
.right
;
433 gfxPoint
pt(PresContext()->AppUnitsToGfxUnits(bp
.left
+ aPt
.x
),
434 PresContext()->AppUnitsToGfxUnits(bp
.top
+ aPt
.y
));
435 gfxSize
size(PresContext()->AppUnitsToGfxUnits(innerWidth
), aSize
);
436 nsCSSRendering::PaintDecorationLine(aCtx
, aColor
, pt
, size
, aAscent
, aOffset
,
437 aDecoration
, nsCSSRendering::DECORATION_STYLE_SOLID
);
441 nsHTMLContainerFrame::AdjustForTextIndent(const nsLineBox
* aLine
,
445 // This function is not for us.
446 // It allows nsBlockFrame to adjust the width/X position of its
447 // shadowed decorations if a text-indent rule is in effect.
451 nsHTMLContainerFrame::GetTextDecorations(nsPresContext
* aPresContext
,
453 PRUint8
& aDecorations
,
454 nscolor
& aUnderColor
,
456 nscolor
& aStrikeColor
)
458 aDecorations
= NS_STYLE_TEXT_DECORATION_NONE
;
459 if (!mStyleContext
->HasTextDecorations()) {
460 // This is a necessary, but not sufficient, condition for text
466 aDecorations
= this->GetStyleTextReset()->mTextDecoration
&
467 NS_STYLE_TEXT_DECORATION_LINES_MASK
;
469 nscolor color
= this->GetVisitedDependentColor(eCSSProperty_color
);
472 aStrikeColor
= color
;
476 // We want to ignore a text-decoration from an ancestor frame that
477 // is redundant with one from a descendant frame. This isn't just
478 // an optimization; the descendant frame's color specification
479 // must win. At any point in the loop below, this variable
480 // indicates which decorations we are still paying attention to;
481 // it starts set to all possible decorations.
482 PRUint8 decorMask
= NS_STYLE_TEXT_DECORATION_LINES_MASK
;
485 for (nsIFrame
* frame
= this; frame
; frame
= frame
->GetParent()) {
486 PRUint8 decors
= frame
->GetStyleTextReset()->mTextDecoration
& decorMask
;
488 // A *new* text-decoration is found.
489 nscolor color
= frame
->GetVisitedDependentColor(eCSSProperty_color
);
491 if (NS_STYLE_TEXT_DECORATION_UNDERLINE
& decors
) {
493 decorMask
&= ~NS_STYLE_TEXT_DECORATION_UNDERLINE
;
494 aDecorations
|= NS_STYLE_TEXT_DECORATION_UNDERLINE
;
496 if (NS_STYLE_TEXT_DECORATION_OVERLINE
& decors
) {
498 decorMask
&= ~NS_STYLE_TEXT_DECORATION_OVERLINE
;
499 aDecorations
|= NS_STYLE_TEXT_DECORATION_OVERLINE
;
501 if (NS_STYLE_TEXT_DECORATION_LINE_THROUGH
& decors
) {
502 aStrikeColor
= color
;
503 decorMask
&= ~NS_STYLE_TEXT_DECORATION_LINE_THROUGH
;
504 aDecorations
|= NS_STYLE_TEXT_DECORATION_LINE_THROUGH
;
507 // If all possible decorations have now been specified, no
508 // further ancestor frames can affect the rendering.
513 // CSS2.1 16.3.1 specifies that this property is not always
514 // inherited from ancestor boxes (frames in our terminology):
516 // When specified on an inline element, [the
517 // text-decoration property] affects all the boxes
518 // generated by that element; for all other elements, the
519 // decorations are propagated to an anonymous inline box
520 // that wraps all the in-flow inline children of the
521 // element, and to any block-level in-flow descendants. It
522 // is not, however, further propagated to floating and
523 // absolutely positioned descendants, nor to the contents
524 // of 'inline-table' and 'inline-block' descendants.
526 // So do not look at the ancestor frame if this frame is any of
527 // the above. This check is at the bottom of the loop because
528 // even if it's true we still want to look at decorations on the
530 const nsStyleDisplay
* styleDisplay
= frame
->GetStyleDisplay();
531 if (styleDisplay
->IsFloating() ||
532 styleDisplay
->IsAbsolutelyPositioned() ||
533 styleDisplay
->IsInlineOutside()) {
540 // If this frame contains no text, we're required to ignore this property
541 if (!HasTextFrameDescendantOrInFlow(this)) {
542 aDecorations
= NS_STYLE_TEXT_DECORATION_NONE
;
548 HasTextFrameDescendant(nsIFrame
* aParent
)
550 for (nsIFrame
* kid
= aParent
->GetFirstChild(nsnull
); kid
;
551 kid
= kid
->GetNextSibling())
553 if (kid
->GetType() == nsGkAtoms::textFrame
) {
554 // This is only a candidate. We need to determine if this text
555 // frame is empty, as in containing only (non-pre) whitespace.
557 if (!kid
->IsEmpty()) {
561 if (HasTextFrameDescendant(kid
)) {
569 HasTextFrameDescendantOrInFlow(nsIFrame
* aFrame
)
571 for (nsIFrame
*f
= aFrame
->GetFirstInFlow(); f
; f
= f
->GetNextInFlow()) {
572 if (HasTextFrameDescendant(f
))
579 * Create a next-in-flow for aFrame. Will return the newly created
580 * frame in aNextInFlowResult <b>if and only if</b> a new frame is
581 * created; otherwise nsnull is returned in aNextInFlowResult.
584 nsHTMLContainerFrame::CreateNextInFlow(nsPresContext
* aPresContext
,
586 nsIFrame
*& aNextInFlowResult
)
588 NS_PRECONDITION(GetType() != nsGkAtoms::blockFrame
,
589 "you should have called nsBlockFrame::CreateContinuationFor instead");
590 NS_PRECONDITION(mFrames
.ContainsFrame(aFrame
), "expected an in-flow child frame");
592 aNextInFlowResult
= nsnull
;
594 nsIFrame
* nextInFlow
= aFrame
->GetNextInFlow();
595 if (nsnull
== nextInFlow
) {
596 // Create a continuation frame for the child frame and insert it
597 // into our child list.
598 nsresult rv
= aPresContext
->PresShell()->FrameConstructor()->
599 CreateContinuingFrame(aPresContext
, aFrame
, this, &nextInFlow
);
603 mFrames
.InsertFrame(nsnull
, aFrame
, nextInFlow
);
605 NS_FRAME_LOG(NS_FRAME_TRACE_NEW_FRAMES
,
606 ("nsHTMLContainerFrame::CreateNextInFlow: frame=%p nextInFlow=%p",
607 aFrame
, nextInFlow
));
609 aNextInFlowResult
= nextInFlow
;
615 ReparentFrameViewTo(nsIFrame
* aFrame
,
616 nsIViewManager
* aViewManager
,
617 nsIView
* aNewParentView
,
618 nsIView
* aOldParentView
)
621 // XXX What to do about placeholder views for "position: fixed" elements?
622 // They should be reparented too.
624 // Does aFrame have a view?
625 if (aFrame
->HasView()) {
627 if (aFrame
->GetType() == nsGkAtoms::menuPopupFrame
) {
628 // This view must be parented by the root view, don't reparent it.
632 nsIView
* view
= aFrame
->GetView();
633 // Verify that the current parent view is what we think it is
634 //nsIView* parentView;
635 //NS_ASSERTION(parentView == aOldParentView, "unexpected parent view");
637 aViewManager
->RemoveChild(view
);
639 // The view will remember the Z-order and other attributes that have been set on it.
640 nsIView
* insertBefore
= nsLayoutUtils::FindSiblingViewFor(aNewParentView
, aFrame
);
641 aViewManager
->InsertChild(aNewParentView
, view
, insertBefore
, insertBefore
!= nsnull
);
643 PRInt32 listIndex
= 0;
644 nsIAtom
* listName
= nsnull
;
645 // This loop iterates through every child list name, and also
646 // executes once with listName == nsnull.
648 // Iterate the child frames, and check each child frame to see if it has
650 nsIFrame
* childFrame
= aFrame
->GetFirstChild(listName
);
651 for (; childFrame
; childFrame
= childFrame
->GetNextSibling()) {
652 ReparentFrameViewTo(childFrame
, aViewManager
,
653 aNewParentView
, aOldParentView
);
655 listName
= aFrame
->GetAdditionalChildListName(listIndex
++);
663 nsHTMLContainerFrame::ReparentFrameView(nsPresContext
* aPresContext
,
664 nsIFrame
* aChildFrame
,
665 nsIFrame
* aOldParentFrame
,
666 nsIFrame
* aNewParentFrame
)
668 NS_PRECONDITION(aChildFrame
, "null child frame pointer");
669 NS_PRECONDITION(aOldParentFrame
, "null old parent frame pointer");
670 NS_PRECONDITION(aNewParentFrame
, "null new parent frame pointer");
671 NS_PRECONDITION(aOldParentFrame
!= aNewParentFrame
, "same old and new parent frame");
673 // See if either the old parent frame or the new parent frame have a view
674 while (!aOldParentFrame
->HasView() && !aNewParentFrame
->HasView()) {
675 // Walk up both the old parent frame and the new parent frame nodes
676 // stopping when we either find a common parent or views for one
677 // or both of the frames.
679 // This works well in the common case where we push/pull and the old parent
680 // frame and the new parent frame are part of the same flow. They will
681 // typically be the same distance (height wise) from the
682 aOldParentFrame
= aOldParentFrame
->GetParent();
683 aNewParentFrame
= aNewParentFrame
->GetParent();
685 // We should never walk all the way to the root frame without finding
687 NS_ASSERTION(aOldParentFrame
&& aNewParentFrame
, "didn't find view");
689 // See if we reached a common ancestor
690 if (aOldParentFrame
== aNewParentFrame
) {
695 // See if we found a common parent frame
696 if (aOldParentFrame
== aNewParentFrame
) {
697 // We found a common parent and there are no views between the old parent
698 // and the common parent or the new parent frame and the common parent.
699 // Because neither the old parent frame nor the new parent frame have views,
700 // then any child views don't need reparenting
704 // We found views for one or both of the ancestor frames before we
705 // found a common ancestor.
706 nsIView
* oldParentView
= aOldParentFrame
->GetClosestView();
707 nsIView
* newParentView
= aNewParentFrame
->GetClosestView();
709 // See if the old parent frame and the new parent frame are in the
710 // same view sub-hierarchy. If they are then we don't have to do
712 if (oldParentView
!= newParentView
) {
713 // They're not so we need to reparent any child views
714 return ReparentFrameViewTo(aChildFrame
, oldParentView
->GetViewManager(), newParentView
,
722 nsHTMLContainerFrame::ReparentFrameViewList(nsPresContext
* aPresContext
,
723 const nsFrameList
& aChildFrameList
,
724 nsIFrame
* aOldParentFrame
,
725 nsIFrame
* aNewParentFrame
)
727 NS_PRECONDITION(aChildFrameList
.NotEmpty(), "empty child frame list");
728 NS_PRECONDITION(aOldParentFrame
, "null old parent frame pointer");
729 NS_PRECONDITION(aNewParentFrame
, "null new parent frame pointer");
730 NS_PRECONDITION(aOldParentFrame
!= aNewParentFrame
, "same old and new parent frame");
732 // See if either the old parent frame or the new parent frame have a view
733 while (!aOldParentFrame
->HasView() && !aNewParentFrame
->HasView()) {
734 // Walk up both the old parent frame and the new parent frame nodes
735 // stopping when we either find a common parent or views for one
736 // or both of the frames.
738 // This works well in the common case where we push/pull and the old parent
739 // frame and the new parent frame are part of the same flow. They will
740 // typically be the same distance (height wise) from the
741 aOldParentFrame
= aOldParentFrame
->GetParent();
742 aNewParentFrame
= aNewParentFrame
->GetParent();
744 // We should never walk all the way to the root frame without finding
746 NS_ASSERTION(aOldParentFrame
&& aNewParentFrame
, "didn't find view");
748 // See if we reached a common ancestor
749 if (aOldParentFrame
== aNewParentFrame
) {
755 // See if we found a common parent frame
756 if (aOldParentFrame
== aNewParentFrame
) {
757 // We found a common parent and there are no views between the old parent
758 // and the common parent or the new parent frame and the common parent.
759 // Because neither the old parent frame nor the new parent frame have views,
760 // then any child views don't need reparenting
764 // We found views for one or both of the ancestor frames before we
765 // found a common ancestor.
766 nsIView
* oldParentView
= aOldParentFrame
->GetClosestView();
767 nsIView
* newParentView
= aNewParentFrame
->GetClosestView();
769 // See if the old parent frame and the new parent frame are in the
770 // same view sub-hierarchy. If they are then we don't have to do
772 if (oldParentView
!= newParentView
) {
773 nsIViewManager
* viewManager
= oldParentView
->GetViewManager();
775 // They're not so we need to reparent any child views
776 for (nsFrameList::Enumerator
e(aChildFrameList
); !e
.AtEnd(); e
.Next()) {
777 ReparentFrameViewTo(e
.get(), viewManager
, newParentView
, oldParentView
);
785 nsHTMLContainerFrame::CreateViewForFrame(nsIFrame
* aFrame
,
788 if (aFrame
->HasView()) {
792 // If we don't yet have a view, see if we need a view
793 if (!aForce
&& !aFrame
->NeedsView()) {
798 nsIView
* parentView
= aFrame
->GetParent()->GetClosestView();
799 NS_ASSERTION(parentView
, "no parent with view");
801 nsIViewManager
* viewManager
= parentView
->GetViewManager();
802 NS_ASSERTION(viewManager
, "null view manager");
805 nsIView
* view
= viewManager
->CreateView(aFrame
->GetRect(), parentView
);
807 return NS_ERROR_OUT_OF_MEMORY
;
809 SyncFrameViewProperties(aFrame
->PresContext(), aFrame
, nsnull
, view
);
811 nsIView
* insertBefore
= nsLayoutUtils::FindSiblingViewFor(parentView
, aFrame
);
812 // we insert this view 'above' the insertBefore view, unless insertBefore is null,
813 // in which case we want to call with aAbove == PR_FALSE to insert at the beginning
815 viewManager
->InsertChild(parentView
, view
, insertBefore
, insertBefore
!= nsnull
);
817 // REVIEW: Don't create a widget for fixed-pos elements anymore.
818 // ComputeRepaintRegionForCopy will calculate the right area to repaint
820 // Reparent views on any child frames (or their descendants) to this
821 // view. We can just call ReparentFrameViewTo on this frame because
822 // we know this frame has no view, so it will crawl the children. Also,
823 // we know that any descendants with views must have 'parentView' as their
825 ReparentFrameViewTo(aFrame
, viewManager
, view
, parentView
);
828 aFrame
->SetView(view
);
830 NS_FRAME_LOG(NS_FRAME_TRACE_CALLS
,
831 ("nsHTMLContainerFrame::CreateViewForFrame: frame=%p view=%p",
836 NS_IMPL_FRAMEARENA_HELPERS(nsHTMLContainerFrame
)