gecko-dev/layout/generic/nsSpacerFrame.cpp

191 lines
5.8 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
1998-09-08 22:34:40 +00:00
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
1998-09-08 22:34:40 +00:00
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
1998-09-08 22:34:40 +00:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
1998-09-08 22:34:40 +00:00
#include "nsHTMLParts.h"
#include "nsIHTMLContent.h"
#include "nsFrame.h"
1998-09-15 00:19:49 +00:00
#include "nsLineLayout.h"
1998-09-08 22:34:40 +00:00
#include "nsIPresContext.h"
#include "nsHTMLAtoms.h"
#include "nsUnitConversion.h"
#include "nsIStyleContext.h"
#include "nsStyleConsts.h"
1998-12-20 01:21:23 +00:00
#include "nsINameSpaceManager.h"
1998-09-08 22:34:40 +00:00
// Spacer type's
#define TYPE_WORD 0 // horizontal space
#define TYPE_LINE 1 // line-break + vertical space
#define TYPE_IMAGE 2 // acts like a sized image with nothing to see
class SpacerFrame : public nsFrame {
1998-09-08 22:34:40 +00:00
public:
friend nsresult NS_NewSpacerFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame);
// nsIHTMLReflow
NS_IMETHOD Reflow(nsIPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus);
1998-09-08 22:34:40 +00:00
PRUint8 GetType();
protected:
SpacerFrame();
1998-09-08 22:34:40 +00:00
virtual ~SpacerFrame();
};
nsresult
NS_NewSpacerFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
1998-09-08 22:34:40 +00:00
{
NS_PRECONDITION(aNewFrame, "null OUT ptr");
if (nsnull == aNewFrame) {
return NS_ERROR_NULL_POINTER;
}
SpacerFrame* it = new (aPresShell) SpacerFrame;
if (nsnull == it) {
1998-09-08 22:34:40 +00:00
return NS_ERROR_OUT_OF_MEMORY;
}
*aNewFrame = it;
1998-09-08 22:34:40 +00:00
return NS_OK;
}
SpacerFrame::SpacerFrame()
{
}
1998-09-08 22:34:40 +00:00
SpacerFrame::~SpacerFrame()
{
}
NS_IMETHODIMP
SpacerFrame::Reflow(nsIPresContext* aPresContext,
nsHTMLReflowMetrics& aMetrics,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
1998-09-08 22:34:40 +00:00
{
DO_GLOBAL_REFLOW_COUNT("SpacerFrame", aReflowState.reason);
DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
aStatus = NS_FRAME_COMPLETE;
1998-09-08 22:34:40 +00:00
// By default, we have no area
aMetrics.width = 0;
aMetrics.height = 0;
aMetrics.ascent = 0;
aMetrics.descent = 0;
2000-05-09 05:09:55 +00:00
const nsStylePosition* position;
GetStyleData(eStyleStruct_Position, (const nsStyleStruct*&)position);
PRUint8 type = GetType();
1998-09-08 22:34:40 +00:00
switch (type) {
case TYPE_WORD:
break;
case TYPE_LINE:
2000-05-09 05:09:55 +00:00
aStatus = NS_INLINE_LINE_BREAK_AFTER(NS_FRAME_COMPLETE);
if (eStyleUnit_Coord == position->mHeight.GetUnit()) {
aMetrics.width = position->mHeight.GetCoordValue();
1998-09-08 22:34:40 +00:00
}
2000-05-09 05:09:55 +00:00
aMetrics.ascent = aMetrics.height;
1998-09-08 22:34:40 +00:00
break;
case TYPE_IMAGE:
2000-05-09 05:09:55 +00:00
// width
nsStyleUnit unit = position->mWidth.GetUnit();
if (eStyleUnit_Coord == unit) {
aMetrics.width = position->mWidth.GetCoordValue();
}
else if (eStyleUnit_Percent == unit)
{
if (NS_UNCONSTRAINEDSIZE != aReflowState.availableWidth)
{
float factor = position->mWidth.GetPercentValue();
aMetrics.width = NSToCoordRound (factor * aReflowState.availableWidth);
}
}
// height
unit = position->mHeight.GetUnit();
if (eStyleUnit_Coord == unit) {
aMetrics.height = position->mHeight.GetCoordValue();
}
else if (eStyleUnit_Percent == unit)
{
if (NS_UNCONSTRAINEDSIZE != aReflowState.availableHeight)
{
float factor = position->mHeight.GetPercentValue();
aMetrics.width = NSToCoordRound (factor * aReflowState.availableHeight);
}
}
// accent
1998-09-08 22:34:40 +00:00
aMetrics.ascent = aMetrics.height;
break;
}
if (aMetrics.width || aMetrics.height) {
// Make sure that the other dimension is non-zero
if (!aMetrics.width) aMetrics.width = 1;
if (!aMetrics.height) aMetrics.height = 1;
}
1998-09-08 22:34:40 +00:00
if (nsnull != aMetrics.maxElementSize) {
aMetrics.maxElementSize->width = aMetrics.width;
aMetrics.maxElementSize->height = aMetrics.height;
}
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
return NS_OK;
1998-09-08 22:34:40 +00:00
}
PRUint8
SpacerFrame::GetType()
{
PRUint8 type = TYPE_WORD;
nsAutoString value;
if (NS_CONTENT_ATTR_HAS_VALUE ==
mContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::type, value)) {
1998-09-08 22:34:40 +00:00
if (value.EqualsIgnoreCase("line") ||
value.EqualsIgnoreCase("vert") ||
value.EqualsIgnoreCase("vertical")) {
return TYPE_LINE;
}
if (value.EqualsIgnoreCase("block")) {
return TYPE_IMAGE;
}
}
return type;
}