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 <spacer> element */
40 #include "nsHTMLParts.h"
42 #include "nsLineLayout.h"
43 #include "nsPresContext.h"
44 #include "nsGkAtoms.h"
45 #include "nsStyleConsts.h"
46 #include "nsINameSpaceManager.h"
49 #define TYPE_WORD 0 // horizontal space
50 #define TYPE_LINE 1 // line-break + vertical space
51 #define TYPE_IMAGE 2 // acts like a sized image with nothing to see
53 class SpacerFrame
: public nsFrame
{
55 friend nsIFrame
* NS_NewSpacerFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
);
58 virtual nscoord
GetMinWidth(nsIRenderingContext
*aRenderingContext
);
59 virtual nscoord
GetPrefWidth(nsIRenderingContext
*aRenderingContext
);
60 NS_IMETHOD
Reflow(nsPresContext
* aPresContext
,
61 nsHTMLReflowMetrics
& aDesiredSize
,
62 const nsHTMLReflowState
& aReflowState
,
63 nsReflowStatus
& aStatus
);
68 SpacerFrame(nsStyleContext
* aContext
) : nsFrame(aContext
) {}
69 virtual ~SpacerFrame();
70 void GetDesiredSize(nsHTMLReflowMetrics
& aMetrics
, nsSize aPercentBase
);
74 NS_NewSpacerFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
76 return new (aPresShell
) SpacerFrame(aContext
);
79 SpacerFrame::~SpacerFrame()
84 SpacerFrame::GetMinWidth(nsIRenderingContext
*aRenderingContext
)
86 nsHTMLReflowMetrics metrics
;
87 DISPLAY_MIN_WIDTH(this, metrics
.width
);
88 GetDesiredSize(metrics
, nsSize(0, 0));
93 SpacerFrame::GetPrefWidth(nsIRenderingContext
*aRenderingContext
)
95 nsHTMLReflowMetrics metrics
;
96 DISPLAY_PREF_WIDTH(this, metrics
.width
);
97 GetDesiredSize(metrics
, nsSize(0, 0));
102 SpacerFrame::Reflow(nsPresContext
* aPresContext
,
103 nsHTMLReflowMetrics
& aMetrics
,
104 const nsHTMLReflowState
& aReflowState
,
105 nsReflowStatus
& aStatus
)
107 DO_GLOBAL_REFLOW_COUNT("SpacerFrame");
108 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aMetrics
, aStatus
);
109 aStatus
= NS_FRAME_COMPLETE
;
111 // XXX Bug 379654 Should use containing block size!
112 nsSize
percentBase(aReflowState
.availableWidth
, aReflowState
.availableHeight
);
113 if (percentBase
.width
== NS_UNCONSTRAINEDSIZE
)
114 percentBase
.width
= 0;
115 if (percentBase
.height
== NS_UNCONSTRAINEDSIZE
)
116 percentBase
.height
= 0;
118 if (GetType() == TYPE_LINE
)
119 aStatus
= NS_INLINE_LINE_BREAK_AFTER(NS_FRAME_COMPLETE
);
121 GetDesiredSize(aMetrics
, percentBase
);
123 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aMetrics
);
128 SpacerFrame::GetDesiredSize(nsHTMLReflowMetrics
& aMetrics
, nsSize aPercentBase
)
130 // By default, we have no area
134 // XXX Bug 379654 This code doesn't handle some value types for width
135 // and height, doesn't handle min/max-width/height, doesn't handle
136 // border and padding, doesn't handle 'ch' units, doesn't handle the
137 // enumerated values on width, etc. But it probably doesn't much
140 const nsStylePosition
* position
= GetStylePosition();
142 PRUint8 type
= GetType();
148 if (eStyleUnit_Coord
== position
->mHeight
.GetUnit()) {
149 aMetrics
.height
= position
->mHeight
.GetCoordValue();
155 nsStyleUnit unit
= position
->mWidth
.GetUnit();
156 if (eStyleUnit_Coord
== unit
) {
157 aMetrics
.width
= position
->mWidth
.GetCoordValue();
159 else if (eStyleUnit_Percent
== unit
)
161 float factor
= position
->mWidth
.GetPercentValue();
162 aMetrics
.width
= NSToCoordRound(factor
* aPercentBase
.width
);
166 unit
= position
->mHeight
.GetUnit();
167 if (eStyleUnit_Coord
== unit
) {
168 aMetrics
.height
= position
->mHeight
.GetCoordValue();
170 else if (eStyleUnit_Percent
== unit
)
172 float factor
= position
->mHeight
.GetPercentValue();
173 aMetrics
.height
= NSToCoordRound(factor
* aPercentBase
.height
);
178 if (aMetrics
.width
|| aMetrics
.height
) {
179 // Make sure that the other dimension is non-zero
180 if (!aMetrics
.width
) aMetrics
.width
= 1;
181 if (!aMetrics
.height
) aMetrics
.height
= 1;
186 SpacerFrame::GetType()
188 PRUint8 type
= TYPE_WORD
;
189 static nsIContent::AttrValuesArray strings
[] =
190 {&nsGkAtoms::line
, &nsGkAtoms::vert
, &nsGkAtoms::vertical
,
191 &nsGkAtoms::block
, nsnull
};
192 switch (mContent
->FindAttrValueIn(kNameSpaceID_None
, nsGkAtoms::type
,
193 strings
, eIgnoreCase
)) {