Back to index

lightning-sunbird  0.9+nobinonly
nsStyleChangeList.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 
00038 #include "nsStyleChangeList.h"
00039 #include "nsStyleConsts.h"
00040 #include "nsIFrame.h"
00041 #include "nsIContent.h"
00042 #include "nsCRT.h"
00043 
00044 static const PRUint32 kGrowArrayBy = 10;
00045 
00046 MOZ_DECL_CTOR_COUNTER(nsStyleChangeList)
00047 
00048 nsStyleChangeList::nsStyleChangeList(void)
00049   : mArray(mBuffer),
00050     mArraySize(kStyleChangeBufferSize),
00051     mCount(0)
00052 {
00053   MOZ_COUNT_CTOR(nsStyleChangeList);
00054 }
00055 
00056 nsStyleChangeList::~nsStyleChangeList(void)
00057 {
00058   MOZ_COUNT_DTOR(nsStyleChangeList);
00059   Clear();
00060 }
00061 
00062 nsresult 
00063 nsStyleChangeList::ChangeAt(PRInt32 aIndex, nsIFrame*& aFrame, nsIContent*& aContent, 
00064                             nsChangeHint& aHint) const
00065 {
00066   if ((0 <= aIndex) && (aIndex < mCount)) {
00067     aFrame = mArray[aIndex].mFrame;
00068     aContent = mArray[aIndex].mContent;
00069     aHint = mArray[aIndex].mHint;
00070     return NS_OK;
00071   }
00072   return NS_ERROR_ILLEGAL_VALUE;
00073 }
00074 
00075 nsresult 
00076 nsStyleChangeList::ChangeAt(PRInt32 aIndex, const nsStyleChangeData** aChangeData) const
00077 {
00078   if ((0 <= aIndex) && (aIndex < mCount)) {
00079     *aChangeData = &mArray[aIndex];
00080     return NS_OK;
00081   }
00082   return NS_ERROR_ILLEGAL_VALUE;
00083 }
00084 
00085 nsresult 
00086 nsStyleChangeList::AppendChange(nsIFrame* aFrame, nsIContent* aContent, nsChangeHint aHint)
00087 {
00088   NS_ASSERTION(aFrame || (aHint & nsChangeHint_ReconstructFrame),
00089                "must have frame");
00090   NS_ASSERTION(aContent || !(aHint & nsChangeHint_ReconstructFrame),
00091                "must have content");
00092   NS_ASSERTION(!aContent || aContent->IsContentOfType(nsIContent::eELEMENT),
00093                "Shouldn't be trying to restyle non-elements directly");
00094 
00095   if ((0 < mCount) && (aHint & nsChangeHint_ReconstructFrame)) { // filter out all other changes for same content
00096     if (aContent) {
00097       PRInt32 index = mCount;
00098       while (0 < index--) {
00099         if (aContent == mArray[index].mContent) { // remove this change
00100           mCount--;
00101           if (index < mCount) { // move later changes down
00102             ::memmove(&mArray[index], &mArray[index + 1], 
00103                       (mCount - index) * sizeof(nsStyleChangeData));
00104           }
00105         }
00106       }
00107     }
00108   }
00109 
00110   PRInt32 last = mCount - 1;
00111   if ((0 < mCount) && aFrame && (aFrame == mArray[last].mFrame)) { // same as last frame
00112     NS_UpdateHint(mArray[last].mHint, aHint);
00113   }
00114   else {
00115     if (mCount == mArraySize) {
00116       PRInt32 newSize = mArraySize + kGrowArrayBy;
00117       nsStyleChangeData* newArray = new nsStyleChangeData[newSize];
00118       if (newArray) {
00119         memcpy(newArray, mArray, mCount * sizeof(nsStyleChangeData));
00120         if (mArray != mBuffer) {
00121           delete [] mArray;
00122         }
00123         mArray = newArray;
00124         mArraySize = newSize;
00125       }
00126       else {
00127         return NS_ERROR_OUT_OF_MEMORY;
00128       }
00129     }
00130     mArray[mCount].mFrame = aFrame;
00131     mArray[mCount].mContent = aContent;
00132     mArray[mCount].mHint = aHint;
00133     mCount++;
00134   }
00135   return NS_OK;
00136 }
00137 
00138 void 
00139 nsStyleChangeList::Clear() 
00140 {
00141   if (mArray != mBuffer) {
00142     delete [] mArray;
00143     mArray = mBuffer;
00144     mArraySize = kStyleChangeBufferSize;
00145   }
00146   mCount = 0;
00147 }
00148