Bug 574454 - Cleanup nsNativeThemeWin's GetMinimumWidgetSize a bit. r=roc.
[mozilla-central.git] / layout / generic / nsSpacerFrame.cpp
blob7921b8ddcaa1e2203cccad05256f6a17a1e3d5ea
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 <spacer> element */
40 #include "nsHTMLParts.h"
41 #include "nsFrame.h"
42 #include "nsLineLayout.h"
43 #include "nsPresContext.h"
44 #include "nsGkAtoms.h"
45 #include "nsStyleConsts.h"
46 #include "nsINameSpaceManager.h"
48 // Spacer type's
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 public:
56 NS_DECL_FRAMEARENA_HELPERS
58 friend nsIFrame* NS_NewSpacerFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
60 // nsIHTMLReflow
61 virtual nscoord GetMinWidth(nsIRenderingContext *aRenderingContext);
62 virtual nscoord GetPrefWidth(nsIRenderingContext *aRenderingContext);
63 NS_IMETHOD Reflow(nsPresContext* aPresContext,
64 nsHTMLReflowMetrics& aDesiredSize,
65 const nsHTMLReflowState& aReflowState,
66 nsReflowStatus& aStatus);
68 PRUint8 GetSpacerType();
70 protected:
71 SpacerFrame(nsStyleContext* aContext) : nsFrame(aContext) {}
72 virtual ~SpacerFrame();
73 void GetDesiredSize(nsHTMLReflowMetrics& aMetrics, nsSize aPercentBase);
76 nsIFrame*
77 NS_NewSpacerFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
79 #ifdef DEBUG
80 const nsStyleDisplay* disp = aContext->GetStyleDisplay();
81 NS_ASSERTION(!disp->IsAbsolutelyPositioned() && !disp->IsFloating(),
82 "Spacers should not be positioned and should not float");
83 #endif
85 return new (aPresShell) SpacerFrame(aContext);
88 NS_IMPL_FRAMEARENA_HELPERS(SpacerFrame)
90 SpacerFrame::~SpacerFrame()
94 /* virtual */ nscoord
95 SpacerFrame::GetMinWidth(nsIRenderingContext *aRenderingContext)
97 nsHTMLReflowMetrics metrics;
98 DISPLAY_MIN_WIDTH(this, metrics.width);
99 GetDesiredSize(metrics, nsSize(0, 0));
100 return metrics.width;
103 /* virtual */ nscoord
104 SpacerFrame::GetPrefWidth(nsIRenderingContext *aRenderingContext)
106 nsHTMLReflowMetrics metrics;
107 DISPLAY_PREF_WIDTH(this, metrics.width);
108 GetDesiredSize(metrics, nsSize(0, 0));
109 return metrics.width;
112 NS_IMETHODIMP
113 SpacerFrame::Reflow(nsPresContext* aPresContext,
114 nsHTMLReflowMetrics& aMetrics,
115 const nsHTMLReflowState& aReflowState,
116 nsReflowStatus& aStatus)
118 DO_GLOBAL_REFLOW_COUNT("SpacerFrame");
119 DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
120 aStatus = NS_FRAME_COMPLETE;
122 // XXX Bug 379654 Should use containing block size!
123 nsSize percentBase(aReflowState.availableWidth, aReflowState.availableHeight);
124 if (percentBase.width == NS_UNCONSTRAINEDSIZE)
125 percentBase.width = 0;
126 if (percentBase.height == NS_UNCONSTRAINEDSIZE)
127 percentBase.height = 0;
129 if (GetSpacerType() == TYPE_LINE)
130 aStatus = NS_INLINE_LINE_BREAK_AFTER(NS_FRAME_COMPLETE);
132 GetDesiredSize(aMetrics, percentBase);
134 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
135 return NS_OK;
138 void
139 SpacerFrame::GetDesiredSize(nsHTMLReflowMetrics& aMetrics, nsSize aPercentBase)
141 // By default, we have no area
142 aMetrics.width = 0;
143 aMetrics.height = 0;
145 // XXX Bug 379654 This code doesn't handle some value types for width
146 // and height, doesn't handle min/max-width/height, doesn't handle
147 // border and padding, doesn't handle 'ch' units, doesn't handle the
148 // enumerated values on width, etc. But it probably doesn't much
149 // matter.
151 const nsStylePosition* position = GetStylePosition();
153 PRUint8 type = GetSpacerType();
154 switch (type) {
155 case TYPE_WORD:
156 break;
158 case TYPE_LINE:
159 if (eStyleUnit_Coord == position->mHeight.GetUnit()) {
160 aMetrics.height = position->mHeight.GetCoordValue();
162 break;
164 case TYPE_IMAGE:
165 // width
166 nsStyleUnit unit = position->mWidth.GetUnit();
167 if (eStyleUnit_Coord == unit) {
168 aMetrics.width = position->mWidth.GetCoordValue();
170 else if (eStyleUnit_Percent == unit)
172 float factor = position->mWidth.GetPercentValue();
173 aMetrics.width = NSToCoordRound(factor * aPercentBase.width);
176 // height
177 unit = position->mHeight.GetUnit();
178 if (eStyleUnit_Coord == unit) {
179 aMetrics.height = position->mHeight.GetCoordValue();
181 else if (eStyleUnit_Percent == unit)
183 float factor = position->mHeight.GetPercentValue();
184 aMetrics.height = NSToCoordRound(factor * aPercentBase.height);
186 break;
189 if (aMetrics.width || aMetrics.height) {
190 // Make sure that the other dimension is non-zero
191 if (!aMetrics.width) aMetrics.width = 1;
192 if (!aMetrics.height) aMetrics.height = 1;
196 PRUint8
197 SpacerFrame::GetSpacerType()
199 PRUint8 type = TYPE_WORD;
200 static nsIContent::AttrValuesArray strings[] =
201 {&nsGkAtoms::line, &nsGkAtoms::vert, &nsGkAtoms::vertical,
202 &nsGkAtoms::block, nsnull};
203 switch (mContent->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type,
204 strings, eIgnoreCase)) {
205 case 0:
206 case 1:
207 case 2:
208 return TYPE_LINE;
209 case 3:
210 return TYPE_IMAGE;
212 return type;