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 "nsIAccessible.h"
54
#include "nsIAccessibilityService.h"
58
#include "nsIContent.h"
59
#include "nsFrameSelection.h"
60
//END INCLUDES FOR SELECTION
62
class BRFrame : public nsFrame {
64
friend nsIFrame* NS_NewBRFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
66
virtual ContentOffsets CalcContentOffsetsFromFramePoint(nsPoint aPoint);
68
virtual PRBool PeekOffsetNoAmount(PRBool aForward, PRInt32* aOffset);
69
virtual PRBool PeekOffsetCharacter(PRBool aForward, PRInt32* aOffset);
70
virtual PRBool PeekOffsetWord(PRBool aForward, PRBool aWordSelectEatSpace, PRBool aIsKeyboardSelect,
71
PRInt32* aOffset, PeekWordState* aState);
73
NS_IMETHOD Reflow(nsPresContext* aPresContext,
74
nsHTMLReflowMetrics& aDesiredSize,
75
const nsHTMLReflowState& aReflowState,
76
nsReflowStatus& aStatus);
77
virtual void AddInlineMinWidth(nsIRenderingContext *aRenderingContext,
78
InlineMinWidthData *aData);
79
virtual void AddInlinePrefWidth(nsIRenderingContext *aRenderingContext,
80
InlinePrefWidthData *aData);
81
virtual nscoord GetMinWidth(nsIRenderingContext *aRenderingContext);
82
virtual nscoord GetPrefWidth(nsIRenderingContext *aRenderingContext);
83
virtual nsIAtom* GetType() const;
85
virtual PRBool IsFrameOfType(PRUint32 aFlags) const
87
return nsFrame::IsFrameOfType(aFlags & ~(nsIFrame::eReplaced |
88
nsIFrame::eLineParticipant));
92
NS_IMETHOD GetAccessible(nsIAccessible** aAccessible);
96
BRFrame(nsStyleContext* aContext) : nsFrame(aContext) {}
101
NS_NewBRFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
103
return new (aPresShell) BRFrame(aContext);
111
BRFrame::Reflow(nsPresContext* aPresContext,
112
nsHTMLReflowMetrics& aMetrics,
113
const nsHTMLReflowState& aReflowState,
114
nsReflowStatus& aStatus)
116
DO_GLOBAL_REFLOW_COUNT("BRFrame");
117
DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
118
aMetrics.height = 0; // BR frames with height 0 are ignored in quirks
119
// mode by nsLineLayout::VerticalAlignFrames .
120
// However, it's not always 0. See below.
124
// Only when the BR is operating in a line-layout situation will it
126
nsLineLayout* ll = aReflowState.mLineLayout;
128
// Note that the compatibility mode check excludes AlmostStandards
129
// mode, since this is the inline box model. See bug 161691.
130
if ( ll->CanPlaceFloatNow() ||
131
aPresContext->CompatibilityMode() == eCompatibility_FullStandards ) {
132
// If we can place a float on the line now it means that the
133
// line is effectively empty (there may be zero sized compressed
134
// white-space frames on the line, but they are to be ignored).
136
// If this frame is going to terminate the line we know
137
// that nothing else will go on the line. Therefore, in this
138
// case, we provide some height for the BR frame so that it
139
// creates some vertical whitespace. It's necessary to use the
140
// line-height rather than the font size because the
141
// quirks-mode fix that doesn't apply the block's min
142
// line-height makes this necessary to make BR cause a line
143
// of the full line-height
145
// We also do this in strict mode because BR should act like a
146
// normal inline frame. That line-height is used is important
147
// here for cases where the line-height is less that 1.
148
nsLayoutUtils::SetFontFromStyle(aReflowState.rendContext, mStyleContext);
149
nsCOMPtr<nsIFontMetrics> fm;
150
aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));
152
nscoord ascent, descent;
153
fm->GetMaxAscent(ascent);
154
fm->GetMaxDescent(descent);
155
nscoord logicalHeight =
156
aReflowState.CalcLineHeight(aReflowState.rendContext, this);
157
nscoord leading = logicalHeight - ascent - descent;
158
aMetrics.height = logicalHeight;
159
aMetrics.ascent = ascent + (leading/2);
162
aMetrics.ascent = aMetrics.height = 0;
165
// XXX temporary until I figure out a better solution; see the
166
// code in nsLineLayout::VerticalAlignFrames that zaps minY/maxY
167
// if the width is zero.
168
// XXX This also fixes bug 10036!
169
// Warning: nsTextControlFrame::CalculateSizeStandard depends on
170
// the following line, see bug 228752.
174
// Return our reflow status
175
PRUint32 breakType = aReflowState.mStyleDisplay->mBreakType;
176
if (NS_STYLE_CLEAR_NONE == breakType) {
177
breakType = NS_STYLE_CLEAR_LINE;
180
aStatus = NS_INLINE_BREAK | NS_INLINE_BREAK_AFTER |
181
NS_INLINE_MAKE_BREAK_TYPE(breakType);
182
ll->SetLineEndsInBR(PR_TRUE);
185
aStatus = NS_FRAME_COMPLETE;
188
aMetrics.mOverflowArea = nsRect(0, 0, aMetrics.width, aMetrics.height);
190
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
195
BRFrame::AddInlineMinWidth(nsIRenderingContext *aRenderingContext,
196
nsIFrame::InlineMinWidthData *aData)
198
aData->ForceBreak(aRenderingContext);
202
BRFrame::AddInlinePrefWidth(nsIRenderingContext *aRenderingContext,
203
nsIFrame::InlinePrefWidthData *aData)
205
aData->ForceBreak(aRenderingContext);
208
/* virtual */ nscoord
209
BRFrame::GetMinWidth(nsIRenderingContext *aRenderingContext)
212
DISPLAY_MIN_WIDTH(this, result);
216
/* virtual */ nscoord
217
BRFrame::GetPrefWidth(nsIRenderingContext *aRenderingContext)
220
DISPLAY_PREF_WIDTH(this, result);
225
BRFrame::GetType() const
227
return nsGkAtoms::brFrame;
230
nsIFrame::ContentOffsets BRFrame::CalcContentOffsetsFromFramePoint(nsPoint aPoint)
232
ContentOffsets offsets;
233
offsets.content = mContent->GetParent();
234
if (offsets.content) {
235
offsets.offset = offsets.content->IndexOf(mContent);
236
offsets.secondaryOffset = offsets.offset;
237
offsets.associateWithNext = PR_TRUE;
243
BRFrame::PeekOffsetNoAmount(PRBool aForward, PRInt32* aOffset)
245
NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
246
PRInt32 startOffset = *aOffset;
247
// If we hit the end of a BR going backwards, go to its beginning and stay there.
248
if (!aForward && startOffset != 0) {
252
// Otherwise, stop if we hit the beginning, continue (forward) if we hit the end.
253
return (startOffset == 0);
257
BRFrame::PeekOffsetCharacter(PRBool aForward, PRInt32* aOffset)
259
NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
260
// Keep going. The actual line jumping will stop us.
265
BRFrame::PeekOffsetWord(PRBool aForward, PRBool aWordSelectEatSpace, PRBool aIsKeyboardSelect,
266
PRInt32* aOffset, PeekWordState* aState)
268
NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
269
// Keep going. The actual line jumping will stop us.
274
NS_IMETHODIMP BRFrame::GetAccessible(nsIAccessible** aAccessible)
276
nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
278
return accService->CreateHTMLBRAccessible(static_cast<nsIFrame*>(this), aAccessible);
280
return NS_ERROR_FAILURE;