Back to index

lightning-sunbird  0.9+nobinonly
nsCounterManager.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 // vim:cindent:ai:sw=4:ts=4:et:
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsCounterManager.
00017  *
00018  * The Initial Developer of the Original Code is the Mozilla Foundation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   L. David Baron <dbaron@dbaron.org> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCounterManager.h"
00040 #include "nsBulletFrame.h" // legacy location for list style type to text code
00041 #include "nsContentUtils.h"
00042 
00043 // assign the correct |mValueAfter| value to a node that has been inserted
00044 // Should be called immediately after calling |Insert|.
00045 void nsCounterUseNode::Calc(nsCounterList *aList)
00046 {
00047     NS_ASSERTION(!aList->IsDirty(),
00048                  "Why are we calculating with a dirty list?");
00049     mValueAfter = aList->ValueBefore(this);
00050 }
00051 
00052 // assign the correct |mValueAfter| value to a node that has been inserted
00053 // Should be called immediately after calling |Insert|.
00054 void nsCounterChangeNode::Calc(nsCounterList *aList)
00055 {
00056     NS_ASSERTION(!aList->IsDirty(),
00057                  "Why are we calculating with a dirty list?");
00058     if (mType == RESET) {
00059         mValueAfter = mChangeValue;
00060     } else {
00061         NS_ASSERTION(mType == INCREMENT, "invalid type");
00062         mValueAfter = aList->ValueBefore(this) + mChangeValue;
00063     }
00064 }
00065 
00066 // The text that should be displayed for this counter.
00067 void
00068 nsCounterUseNode::GetText(nsString& aResult)
00069 {
00070     aResult.Truncate();
00071 
00072     nsAutoVoidArray stack;
00073     stack.AppendElement(NS_STATIC_CAST(nsCounterNode*, this));
00074 
00075     if (mAllCounters && mScopeStart)
00076         for (nsCounterNode *n = mScopeStart; n->mScopePrev; n = n->mScopeStart)
00077             stack.AppendElement(n->mScopePrev);
00078 
00079     PRInt32 style = mCounterStyle->Item(mAllCounters ? 2 : 1).GetIntValue();
00080     const PRUnichar* separator;
00081     if (mAllCounters)
00082         separator = mCounterStyle->Item(1).GetStringBufferValue();
00083 
00084     for (PRInt32 i = stack.Count() - 1;; --i) {
00085         nsCounterNode *n = NS_STATIC_CAST(nsCounterNode*, stack[i]);
00086         nsBulletFrame::AppendCounterText(style, n->mValueAfter, aResult);
00087         if (i == 0)
00088             break;
00089         NS_ASSERTION(mAllCounters, "yikes, separator is uninitalized");
00090         aResult.Append(separator);
00091     }
00092 }
00093 
00094 void
00095 nsCounterList::SetScope(nsCounterNode *aNode)
00096 {
00097     // This function is responsible for setting |mScopeStart| and
00098     // |mScopePrev| (whose purpose is described in nsCounterManager.h).
00099     // We do this by starting from the node immediately preceding
00100     // |aNode| in content tree order, which is reasonably likely to be
00101     // the previous element in our scope (or, for a reset, the previous
00102     // element in the containing scope, which is what we want).  If
00103     // we're not in the same scope that it is, then it's too deep in the
00104     // frame tree, so we walk up parent scopes until we find something
00105     // appropriate.
00106 
00107     if (aNode == First()) {
00108         aNode->mScopeStart = nsnull;
00109         aNode->mScopePrev = nsnull;
00110         return;
00111     }
00112 
00113     // Get the content node for aNode's rendering object's *parent*,
00114     // since scope includes siblings, so we want a descendant check on
00115     // parents.  If aNode is for a pseudo-element, then the parent
00116     // rendering object is the frame's content; if aNode is for an
00117     // element, then the parent rendering object is the frame's
00118     // content's parent.
00119     nsIContent *nodeContent = aNode->mPseudoFrame->GetContent();
00120     if (!aNode->mPseudoFrame->GetStyleContext()->GetPseudoType()) {
00121         nodeContent = nodeContent->GetParent();
00122     }
00123 
00124     for (nsCounterNode *prev = Prev(aNode), *start;
00125          prev; prev = start->mScopePrev) {
00126         // If |prev| starts a scope (because it's a real or implied
00127         // reset), we want it as the scope start rather than the start
00128         // of its enclosing scope.  Otherwise, there's no enclosing
00129         // scope, so the next thing in prev's scope shares its scope
00130         // start.
00131         start = (prev->mType == nsCounterNode::RESET || !prev->mScopeStart)
00132                   ? prev : prev->mScopeStart;
00133 
00134         // |startContent| is analogous to |nodeContent| (see above).
00135         nsIContent *startContent = start->mPseudoFrame->GetContent();
00136         if (!start->mPseudoFrame->GetStyleContext()->GetPseudoType()) {
00137             startContent = startContent->GetParent();
00138         }
00139         NS_ASSERTION(nodeContent || !startContent,
00140                      "null check on startContent should be sufficient to "
00141                      "null check nodeContent as well, since if nodeContent "
00142                      "is for the root, startContent (which is before it) "
00143                      "must be too");
00144 
00145              // A reset's outer scope can't be a scope created by a sibling.
00146         if (!(aNode->mType == nsCounterNode::RESET &&
00147               nodeContent == startContent) &&
00148               // everything is inside the root (except the case above,
00149               // a second reset on the root)
00150             (!startContent ||
00151              nsContentUtils::ContentIsDescendantOf(nodeContent,
00152                                                    startContent))) {
00153             aNode->mScopeStart = start;
00154             aNode->mScopePrev  = prev;
00155             return;
00156         }
00157     }
00158 
00159     aNode->mScopeStart = nsnull;
00160     aNode->mScopePrev  = nsnull;
00161 }
00162 
00163 void
00164 nsCounterList::RecalcAll()
00165 {
00166     mDirty = PR_FALSE;
00167 
00168     nsCounterNode *node = First();
00169     if (!node)
00170         return;
00171 
00172     do {
00173         SetScope(node);
00174         node->Calc(this);
00175 
00176         if (node->mType == nsCounterNode::USE) {
00177             nsCounterUseNode *useNode = node->UseNode();
00178             // Null-check mText, since if the frame constructor isn't
00179             // batching, we could end up here while the node is being
00180             // constructed.
00181             if (useNode->mText) {
00182                 nsAutoString text;
00183                 useNode->GetText(text);
00184                 useNode->mText->SetData(text);
00185             }
00186         }
00187     } while ((node = Next(node)) != First());
00188 }
00189 
00190 nsCounterManager::nsCounterManager()
00191 {
00192     mNames.Init(16);
00193 }
00194 
00195 PRBool
00196 nsCounterManager::AddCounterResetsAndIncrements(nsIFrame *aFrame)
00197 {
00198     const nsStyleContent *styleContent = aFrame->GetStyleContent();
00199     if (!styleContent->CounterIncrementCount() &&
00200         !styleContent->CounterResetCount())
00201         return PR_FALSE;
00202 
00203     // Add in order, resets first, so all the comparisons will be optimized
00204     // for addition at the end of the list.
00205     PRInt32 i, i_end;
00206     PRBool dirty = PR_FALSE;
00207     for (i = 0, i_end = styleContent->CounterResetCount(); i != i_end; ++i)
00208         dirty |= AddResetOrIncrement(aFrame, i,
00209                                      styleContent->GetCounterResetAt(i),
00210                                      nsCounterChangeNode::RESET);
00211     for (i = 0, i_end = styleContent->CounterIncrementCount(); i != i_end; ++i)
00212         dirty |= AddResetOrIncrement(aFrame, i,
00213                                      styleContent->GetCounterIncrementAt(i),
00214                                      nsCounterChangeNode::INCREMENT);
00215     return dirty;
00216 }
00217 
00218 PRBool
00219 nsCounterManager::AddResetOrIncrement(nsIFrame *aFrame, PRInt32 aIndex,
00220                                       const nsStyleCounterData *aCounterData,
00221                                       nsCounterNode::Type aType)
00222 {
00223     nsCounterChangeNode *node =
00224         new nsCounterChangeNode(aFrame, aType, aCounterData->mValue, aIndex);
00225     if (!node)
00226         return PR_FALSE;
00227 
00228     nsCounterList *counterList = CounterListFor(aCounterData->mCounter);
00229     if (!counterList) {
00230         NS_NOTREACHED("CounterListFor failed (should only happen on OOM)");
00231         return PR_FALSE;
00232     }
00233 
00234     counterList->Insert(node);
00235     if (!counterList->IsLast(node)) {
00236         // Tell the caller it's responsible for recalculating the entire
00237         // list.
00238         return PR_TRUE;
00239     }
00240 
00241     // Don't call Calc() if the list is already dirty -- it'll be recalculated
00242     // anyway, and trying to calculate with a dirty list doesn't work.
00243     if (NS_LIKELY(!counterList->IsDirty())) {
00244         node->Calc(counterList);
00245     }
00246     return PR_FALSE;
00247 }
00248 
00249 nsCounterList*
00250 nsCounterManager::CounterListFor(const nsSubstring& aCounterName)
00251 {
00252     // XXX Why doesn't nsTHashtable provide an API that allows us to use
00253     // get/put in one hashtable lookup?
00254     nsCounterList *counterList;
00255     if (!mNames.Get(aCounterName, &counterList)) {
00256         counterList = new nsCounterList();
00257         if (!counterList)
00258             return nsnull;
00259         if (!mNames.Put(aCounterName, counterList)) {
00260             delete counterList;
00261             return nsnull;
00262         }
00263     }
00264     return counterList;
00265 }
00266 
00267 PR_STATIC_CALLBACK(PLDHashOperator)
00268 RecalcDirtyLists(const nsAString& aKey, nsCounterList* aList, void* aClosure)
00269 {
00270     if (aList->IsDirty())
00271         aList->RecalcAll();
00272     return PL_DHASH_NEXT;
00273 }
00274 
00275 void
00276 nsCounterManager::RecalcAll()
00277 {
00278     mNames.EnumerateRead(RecalcDirtyLists, nsnull);
00279 }
00280 
00281 struct DestroyNodesData {
00282     DestroyNodesData(nsIFrame *aFrame)
00283         : mFrame(aFrame)
00284         , mDestroyedAny(PR_FALSE)
00285     {
00286     }
00287 
00288     nsIFrame *mFrame;
00289     PRBool mDestroyedAny;
00290 };
00291 
00292 PR_STATIC_CALLBACK(PLDHashOperator)
00293 DestroyNodesInList(const nsAString& aKey, nsCounterList* aList, void* aClosure)
00294 {
00295     DestroyNodesData *data = NS_STATIC_CAST(DestroyNodesData*, aClosure);
00296     if (aList->DestroyNodesFor(data->mFrame)) {
00297         data->mDestroyedAny = PR_TRUE;
00298         aList->SetDirty();
00299     }
00300     return PL_DHASH_NEXT;
00301 }
00302 
00303 PRBool
00304 nsCounterManager::DestroyNodesFor(nsIFrame *aFrame)
00305 {
00306     DestroyNodesData data(aFrame);
00307     mNames.EnumerateRead(DestroyNodesInList, &data);
00308     return data.mDestroyedAny;
00309 }
00310 
00311 #ifdef DEBUG
00312 PR_STATIC_CALLBACK(PLDHashOperator)
00313 DumpList(const nsAString& aKey, nsCounterList* aList, void* aClosure)
00314 {
00315     printf("Counter named \"%s\":\n", NS_ConvertUTF16toUTF8(aKey).get());
00316     nsCounterNode *node = aList->First();
00317 
00318     if (node) {
00319         PRInt32 i = 0;
00320         do {
00321             const char *types[] = { "RESET", "INCREMENT", "USE" };
00322             printf("  Node #%d @%p frame=%p index=%d type=%s valAfter=%d\n"
00323                    "       scope-start=%p scope-prev=%p",
00324                    i++, (void*)node, (void*)node->mPseudoFrame,
00325                    node->mContentIndex, types[node->mType], node->mValueAfter,
00326                    (void*)node->mScopeStart, (void*)node->mScopePrev);
00327             if (node->mType == nsCounterNode::USE) {
00328                 nsAutoString text;
00329                 node->UseNode()->GetText(text);
00330                 printf(" text=%s", NS_ConvertUTF16toUTF8(text).get());
00331             }
00332             printf("\n");
00333         } while ((node = aList->Next(node)) != aList->First());
00334     }
00335     return PL_DHASH_NEXT;
00336 }
00337 
00338 void
00339 nsCounterManager::Dump()
00340 {
00341     printf("\n\nCounter Manager Lists:\n");
00342     mNames.EnumerateRead(DumpList, nsnull);
00343     printf("\n\n");
00344 }
00345 #endif