Back to index

lightning-sunbird  0.9+nobinonly
nsSpacerFrame.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsHTMLParts.h"
00038 #include "nsFrame.h"
00039 #include "nsLineLayout.h"
00040 #include "nsPresContext.h"
00041 #include "nsHTMLAtoms.h"
00042 #include "nsUnitConversion.h"
00043 #include "nsStyleConsts.h"
00044 #include "nsINameSpaceManager.h"
00045 
00046 // Spacer type's
00047 #define TYPE_WORD  0            // horizontal space
00048 #define TYPE_LINE  1            // line-break + vertical space
00049 #define TYPE_IMAGE 2            // acts like a sized image with nothing to see
00050 
00051 class SpacerFrame : public nsFrame {
00052 public:
00053   friend nsresult NS_NewSpacerFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame);
00054 
00055   // nsIHTMLReflow
00056   NS_IMETHOD Reflow(nsPresContext*          aPresContext,
00057                     nsHTMLReflowMetrics&     aDesiredSize,
00058                     const nsHTMLReflowState& aReflowState,
00059                     nsReflowStatus&          aStatus);
00060 
00061   PRUint8 GetType();
00062 
00063 protected:
00064   SpacerFrame();
00065   virtual ~SpacerFrame();
00066 };
00067 
00068 nsresult
00069 NS_NewSpacerFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
00070 {
00071   NS_PRECONDITION(aNewFrame, "null OUT ptr");
00072   if (nsnull == aNewFrame) {
00073     return NS_ERROR_NULL_POINTER;
00074   }
00075   SpacerFrame* it = new (aPresShell) SpacerFrame;
00076   if (nsnull == it) {
00077     return NS_ERROR_OUT_OF_MEMORY;
00078   }
00079   *aNewFrame = it;
00080   return NS_OK;
00081 }
00082 
00083 SpacerFrame::SpacerFrame()
00084 {
00085 }
00086 
00087 SpacerFrame::~SpacerFrame()
00088 {
00089 }
00090 
00091 NS_IMETHODIMP
00092 SpacerFrame::Reflow(nsPresContext*          aPresContext,
00093                     nsHTMLReflowMetrics&     aMetrics,
00094                     const nsHTMLReflowState& aReflowState,
00095                     nsReflowStatus&          aStatus)
00096 {
00097   DO_GLOBAL_REFLOW_COUNT("SpacerFrame", aReflowState.reason);
00098   DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
00099   aStatus = NS_FRAME_COMPLETE;
00100 
00101   // By default, we have no area
00102   aMetrics.width = 0;
00103   aMetrics.height = 0;
00104   aMetrics.ascent = 0;
00105   aMetrics.descent = 0;
00106 
00107   const nsStylePosition* position = GetStylePosition();
00108 
00109   PRUint8 type = GetType();
00110   switch (type) {
00111   case TYPE_WORD:
00112     break;
00113 
00114   case TYPE_LINE:
00115     aStatus = NS_INLINE_LINE_BREAK_AFTER(NS_FRAME_COMPLETE);
00116     if (eStyleUnit_Coord == position->mHeight.GetUnit()) {
00117       aMetrics.width = position->mHeight.GetCoordValue();
00118     }
00119     aMetrics.ascent = aMetrics.height;
00120     break;
00121 
00122   case TYPE_IMAGE:
00123     // width
00124     nsStyleUnit unit = position->mWidth.GetUnit();
00125     if (eStyleUnit_Coord == unit) {
00126       aMetrics.width = position->mWidth.GetCoordValue();
00127     }
00128     else if (eStyleUnit_Percent == unit) 
00129     {
00130       if (NS_UNCONSTRAINEDSIZE != aReflowState.availableWidth)
00131       {
00132         float factor = position->mWidth.GetPercentValue();
00133         aMetrics.width = NSToCoordRound (factor * aReflowState.availableWidth);
00134       }
00135     }
00136 
00137     // height
00138     unit = position->mHeight.GetUnit();
00139     if (eStyleUnit_Coord == unit) {
00140       aMetrics.height = position->mHeight.GetCoordValue();
00141     }
00142     else if (eStyleUnit_Percent == unit) 
00143     {
00144       if (NS_UNCONSTRAINEDSIZE != aReflowState.availableHeight)
00145       {
00146         float factor = position->mHeight.GetPercentValue();
00147         aMetrics.width = NSToCoordRound (factor * aReflowState.availableHeight);
00148       }
00149     }
00150     // accent
00151     aMetrics.ascent = aMetrics.height;
00152     break;
00153   }
00154 
00155   if (aMetrics.width || aMetrics.height) {
00156     // Make sure that the other dimension is non-zero
00157     if (!aMetrics.width) aMetrics.width = 1;
00158     if (!aMetrics.height) aMetrics.height = 1;
00159   }
00160 
00161   if (aMetrics.mComputeMEW) {
00162     aMetrics.SetMEWToActualWidth(aReflowState.mStylePosition->mWidth.GetUnit());
00163   }
00164 
00165   NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
00166   return NS_OK;
00167 }
00168 
00169 PRUint8
00170 SpacerFrame::GetType()
00171 {
00172   PRUint8 type = TYPE_WORD;
00173   nsAutoString value;
00174   if (NS_CONTENT_ATTR_HAS_VALUE ==
00175       mContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::type, value)) {
00176     if (value.LowerCaseEqualsLiteral("line") ||
00177         value.LowerCaseEqualsLiteral("vert") ||
00178         value.LowerCaseEqualsLiteral("vertical")) {
00179       return TYPE_LINE;
00180     }
00181     if (value.LowerCaseEqualsLiteral("block")) {
00182       return TYPE_IMAGE;
00183     }
00184   }
00185   return type;
00186 }