Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / generic / nsBRFrame.cpp
blob9ffbd69ee7d6527e1ba2f7e9a1fe4eeefab33feb
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 ***** */
38 /* rendering object for HTML <br> elements */
40 #include "nsCOMPtr.h"
41 #include "nsFrame.h"
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"
51 #ifdef ACCESSIBILITY
52 #include "nsIServiceManager.h"
53 #include "nsAccessibilityService.h"
54 #endif
56 //FOR SELECTION
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 {
64 public:
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));
96 #ifdef ACCESSIBILITY
97 virtual already_AddRefed<nsAccessible> CreateAccessible();
98 #endif
100 protected:
101 BRFrame(nsStyleContext* aContext) : nsFrame(aContext) {}
102 virtual ~BRFrame();
105 nsIFrame*
106 NS_NewBRFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
108 return new (aPresShell) BRFrame(aContext);
111 NS_IMPL_FRAMEARENA_HELPERS(BRFrame)
113 BRFrame::~BRFrame()
117 NS_IMETHODIMP
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.
128 aMetrics.width = 0;
129 aMetrics.ascent = 0;
130 RemoveStateBits(BR_USING_CENTERED_FONT_BASELINE);
132 // Only when the BR is operating in a line-layout situation will it
133 // behave like a BR.
134 nsLineLayout* ll = aReflowState.mLineLayout;
135 if (ll) {
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));
157 if (fm) {
158 nscoord logicalHeight = aReflowState.CalcLineHeight();
159 aMetrics.height = logicalHeight;
160 aMetrics.ascent =
161 nsLayoutUtils::GetCenteredFontBaseline(fm, logicalHeight);
162 AddStateBits(BR_USING_CENTERED_FONT_BASELINE);
164 else {
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.
174 aMetrics.width = 1;
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);
187 else {
188 aStatus = NS_FRAME_COMPLETE;
191 aMetrics.SetOverflowAreasToDesiredBounds();
193 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
194 return NS_OK;
197 /* virtual */ void
198 BRFrame::AddInlineMinWidth(nsIRenderingContext *aRenderingContext,
199 nsIFrame::InlineMinWidthData *aData)
201 aData->ForceBreak(aRenderingContext);
204 /* virtual */ void
205 BRFrame::AddInlinePrefWidth(nsIRenderingContext *aRenderingContext,
206 nsIFrame::InlinePrefWidthData *aData)
208 aData->ForceBreak(aRenderingContext);
211 /* virtual */ nscoord
212 BRFrame::GetMinWidth(nsIRenderingContext *aRenderingContext)
214 nscoord result = 0;
215 DISPLAY_MIN_WIDTH(this, result);
216 return result;
219 /* virtual */ nscoord
220 BRFrame::GetPrefWidth(nsIRenderingContext *aRenderingContext)
222 nscoord result = 0;
223 DISPLAY_PREF_WIDTH(this, result);
224 return result;
227 nsIAtom*
228 BRFrame::GetType() const
230 return nsGkAtoms::brFrame;
233 nscoord
234 BRFrame::GetBaseline() const
236 nscoord ascent = 0;
237 nsCOMPtr<nsIFontMetrics> fm;
238 nsLayoutUtils::GetFontMetricsForFrame(this, getter_AddRefs(fm));
239 if (fm) {
240 nscoord logicalHeight = GetRect().height;
241 if (GetStateBits() & BR_USING_CENTERED_FONT_BASELINE) {
242 ascent = nsLayoutUtils::GetCenteredFontBaseline(fm, logicalHeight);
243 } else {
244 fm->GetMaxAscent(ascent);
245 ascent += GetUsedBorderAndPadding().top;
248 return ascent;
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;
260 return offsets;
263 PRBool
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) {
270 *aOffset = 0;
271 return PR_TRUE;
273 // Otherwise, stop if we hit the beginning, continue (forward) if we hit the end.
274 return (startOffset == 0);
277 PRBool
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.
283 return PR_FALSE;
286 PRBool
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.
292 return PR_FALSE;
295 #ifdef ACCESSIBILITY
296 already_AddRefed<nsAccessible>
297 BRFrame::CreateAccessible()
299 nsAccessibilityService* accService = nsIPresShell::AccService();
300 if (!accService) {
301 return nsnull;
303 nsIContent *parent = mContent->GetParent();
304 if (parent &&
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
309 return nsnull;
311 return accService->CreateHTMLBRAccessible(mContent,
312 PresContext()->PresShell());
314 #endif