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.
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 ***** */
38 /* rendering object for HTML <br> elements */
42 #include "nsHTMLParts.h"
43 #include "nsPresContext.h"
44 #include "nsLineLayout.h"
45 #include "nsStyleConsts.h"
46 #include "nsGkAtoms.h"
47 #include "nsIFontMetrics.h"
48 #include "nsIRenderingContext.h"
49 #include "nsLayoutUtils.h"
52 #include "nsIServiceManager.h"
53 #include "nsAccessibilityService.h"
57 #include "nsIContent.h"
58 #include "nsFrameSelection.h"
59 //END INCLUDES FOR SELECTION
61 #define BR_USING_CENTERED_FONT_BASELINE NS_FRAME_STATE_BIT(63)
63 class BRFrame
: public nsFrame
{
65 NS_DECL_FRAMEARENA_HELPERS
67 friend nsIFrame
* NS_NewBRFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
);
69 virtual ContentOffsets
CalcContentOffsetsFromFramePoint(nsPoint aPoint
);
71 virtual PRBool
PeekOffsetNoAmount(PRBool aForward
, PRInt32
* aOffset
);
72 virtual PRBool
PeekOffsetCharacter(PRBool aForward
, PRInt32
* aOffset
,
73 PRBool aRespectClusters
= PR_TRUE
);
74 virtual PRBool
PeekOffsetWord(PRBool aForward
, PRBool aWordSelectEatSpace
, PRBool aIsKeyboardSelect
,
75 PRInt32
* aOffset
, PeekWordState
* aState
);
77 NS_IMETHOD
Reflow(nsPresContext
* aPresContext
,
78 nsHTMLReflowMetrics
& aDesiredSize
,
79 const nsHTMLReflowState
& aReflowState
,
80 nsReflowStatus
& aStatus
);
81 virtual void AddInlineMinWidth(nsIRenderingContext
*aRenderingContext
,
82 InlineMinWidthData
*aData
);
83 virtual void AddInlinePrefWidth(nsIRenderingContext
*aRenderingContext
,
84 InlinePrefWidthData
*aData
);
85 virtual nscoord
GetMinWidth(nsIRenderingContext
*aRenderingContext
);
86 virtual nscoord
GetPrefWidth(nsIRenderingContext
*aRenderingContext
);
87 virtual nsIAtom
* GetType() const;
88 virtual nscoord
GetBaseline() const;
90 virtual PRBool
IsFrameOfType(PRUint32 aFlags
) const
92 return nsFrame::IsFrameOfType(aFlags
& ~(nsIFrame::eReplaced
|
93 nsIFrame::eLineParticipant
));
97 virtual already_AddRefed
<nsAccessible
> CreateAccessible();
101 BRFrame(nsStyleContext
* aContext
) : nsFrame(aContext
) {}
106 NS_NewBRFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
108 return new (aPresShell
) BRFrame(aContext
);
111 NS_IMPL_FRAMEARENA_HELPERS(BRFrame
)
118 BRFrame::Reflow(nsPresContext
* aPresContext
,
119 nsHTMLReflowMetrics
& aMetrics
,
120 const nsHTMLReflowState
& aReflowState
,
121 nsReflowStatus
& aStatus
)
123 DO_GLOBAL_REFLOW_COUNT("BRFrame");
124 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aMetrics
, aStatus
);
125 aMetrics
.height
= 0; // BR frames with height 0 are ignored in quirks
126 // mode by nsLineLayout::VerticalAlignFrames .
127 // However, it's not always 0. See below.
130 RemoveStateBits(BR_USING_CENTERED_FONT_BASELINE
);
132 // Only when the BR is operating in a line-layout situation will it
134 nsLineLayout
* ll
= aReflowState
.mLineLayout
;
136 // Note that the compatibility mode check excludes AlmostStandards
137 // mode, since this is the inline box model. See bug 161691.
138 if ( ll
->LineIsEmpty() ||
139 aPresContext
->CompatibilityMode() == eCompatibility_FullStandards
) {
140 // The line is logically empty; any whitespace is trimmed away.
142 // If this frame is going to terminate the line we know
143 // that nothing else will go on the line. Therefore, in this
144 // case, we provide some height for the BR frame so that it
145 // creates some vertical whitespace. It's necessary to use the
146 // line-height rather than the font size because the
147 // quirks-mode fix that doesn't apply the block's min
148 // line-height makes this necessary to make BR cause a line
149 // of the full line-height
151 // We also do this in strict mode because BR should act like a
152 // normal inline frame. That line-height is used is important
153 // here for cases where the line-height is less than 1.
154 nsLayoutUtils::SetFontFromStyle(aReflowState
.rendContext
, mStyleContext
);
155 nsCOMPtr
<nsIFontMetrics
> fm
;
156 aReflowState
.rendContext
->GetFontMetrics(*getter_AddRefs(fm
));
158 nscoord logicalHeight
= aReflowState
.CalcLineHeight();
159 aMetrics
.height
= logicalHeight
;
161 nsLayoutUtils::GetCenteredFontBaseline(fm
, logicalHeight
);
162 AddStateBits(BR_USING_CENTERED_FONT_BASELINE
);
165 aMetrics
.ascent
= aMetrics
.height
= 0;
168 // XXX temporary until I figure out a better solution; see the
169 // code in nsLineLayout::VerticalAlignFrames that zaps minY/maxY
170 // if the width is zero.
171 // XXX This also fixes bug 10036!
172 // Warning: nsTextControlFrame::CalculateSizeStandard depends on
173 // the following line, see bug 228752.
177 // Return our reflow status
178 PRUint32 breakType
= aReflowState
.mStyleDisplay
->mBreakType
;
179 if (NS_STYLE_CLEAR_NONE
== breakType
) {
180 breakType
= NS_STYLE_CLEAR_LINE
;
183 aStatus
= NS_INLINE_BREAK
| NS_INLINE_BREAK_AFTER
|
184 NS_INLINE_MAKE_BREAK_TYPE(breakType
);
185 ll
->SetLineEndsInBR(PR_TRUE
);
188 aStatus
= NS_FRAME_COMPLETE
;
191 aMetrics
.SetOverflowAreasToDesiredBounds();
193 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aMetrics
);
198 BRFrame::AddInlineMinWidth(nsIRenderingContext
*aRenderingContext
,
199 nsIFrame::InlineMinWidthData
*aData
)
201 aData
->ForceBreak(aRenderingContext
);
205 BRFrame::AddInlinePrefWidth(nsIRenderingContext
*aRenderingContext
,
206 nsIFrame::InlinePrefWidthData
*aData
)
208 aData
->ForceBreak(aRenderingContext
);
211 /* virtual */ nscoord
212 BRFrame::GetMinWidth(nsIRenderingContext
*aRenderingContext
)
215 DISPLAY_MIN_WIDTH(this, result
);
219 /* virtual */ nscoord
220 BRFrame::GetPrefWidth(nsIRenderingContext
*aRenderingContext
)
223 DISPLAY_PREF_WIDTH(this, result
);
228 BRFrame::GetType() const
230 return nsGkAtoms::brFrame
;
234 BRFrame::GetBaseline() const
237 nsCOMPtr
<nsIFontMetrics
> fm
;
238 nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm
));
240 nscoord logicalHeight
= GetRect().height
;
241 if (GetStateBits() & BR_USING_CENTERED_FONT_BASELINE
) {
242 ascent
= nsLayoutUtils::GetCenteredFontBaseline(fm
, logicalHeight
);
244 fm
->GetMaxAscent(ascent
);
245 ascent
+= GetUsedBorderAndPadding().top
;
251 nsIFrame::ContentOffsets
BRFrame::CalcContentOffsetsFromFramePoint(nsPoint aPoint
)
253 ContentOffsets offsets
;
254 offsets
.content
= mContent
->GetParent();
255 if (offsets
.content
) {
256 offsets
.offset
= offsets
.content
->IndexOf(mContent
);
257 offsets
.secondaryOffset
= offsets
.offset
;
258 offsets
.associateWithNext
= PR_TRUE
;
264 BRFrame::PeekOffsetNoAmount(PRBool aForward
, PRInt32
* aOffset
)
266 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
267 PRInt32 startOffset
= *aOffset
;
268 // If we hit the end of a BR going backwards, go to its beginning and stay there.
269 if (!aForward
&& startOffset
!= 0) {
273 // Otherwise, stop if we hit the beginning, continue (forward) if we hit the end.
274 return (startOffset
== 0);
278 BRFrame::PeekOffsetCharacter(PRBool aForward
, PRInt32
* aOffset
,
279 PRBool aRespectClusters
)
281 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
282 // Keep going. The actual line jumping will stop us.
287 BRFrame::PeekOffsetWord(PRBool aForward
, PRBool aWordSelectEatSpace
, PRBool aIsKeyboardSelect
,
288 PRInt32
* aOffset
, PeekWordState
* aState
)
290 NS_ASSERTION (aOffset
&& *aOffset
<= 1, "aOffset out of range");
291 // Keep going. The actual line jumping will stop us.
296 already_AddRefed
<nsAccessible
>
297 BRFrame::CreateAccessible()
299 nsAccessibilityService
* accService
= nsIPresShell::AccService();
303 nsIContent
*parent
= mContent
->GetParent();
305 parent
->IsRootOfNativeAnonymousSubtree() &&
306 parent
->GetChildCount() == 1) {
307 // This <br> is the only node in a text control, therefore it is the hacky
308 // "bogus node" used when there is no text in the control
311 return accService
->CreateHTMLBRAccessible(mContent
,
312 PresContext()->PresShell());