Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / base / nsStyleChangeList.cpp
blobf588b3ad57ae5890327efe52c73fbab141ce3af3
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 ***** */
39 * a list of the recomputation that needs to be done in response to a
40 * style change
43 #include "nsStyleChangeList.h"
44 #include "nsStyleConsts.h"
45 #include "nsIFrame.h"
46 #include "nsIContent.h"
47 #include "nsCRT.h"
49 static const PRUint32 kGrowArrayBy = 10;
51 nsStyleChangeList::nsStyleChangeList()
52 : mArray(mBuffer),
53 mArraySize(kStyleChangeBufferSize),
54 mCount(0)
56 MOZ_COUNT_CTOR(nsStyleChangeList);
59 nsStyleChangeList::~nsStyleChangeList()
61 MOZ_COUNT_DTOR(nsStyleChangeList);
62 Clear();
65 nsresult
66 nsStyleChangeList::ChangeAt(PRInt32 aIndex, nsIFrame*& aFrame, nsIContent*& aContent,
67 nsChangeHint& aHint) const
69 if ((0 <= aIndex) && (aIndex < mCount)) {
70 aFrame = mArray[aIndex].mFrame;
71 aContent = mArray[aIndex].mContent;
72 aHint = mArray[aIndex].mHint;
73 return NS_OK;
75 return NS_ERROR_ILLEGAL_VALUE;
78 nsresult
79 nsStyleChangeList::ChangeAt(PRInt32 aIndex, const nsStyleChangeData** aChangeData) const
81 if ((0 <= aIndex) && (aIndex < mCount)) {
82 *aChangeData = &mArray[aIndex];
83 return NS_OK;
85 return NS_ERROR_ILLEGAL_VALUE;
88 nsresult
89 nsStyleChangeList::AppendChange(nsIFrame* aFrame, nsIContent* aContent, nsChangeHint aHint)
91 NS_ASSERTION(aFrame || (aHint & nsChangeHint_ReconstructFrame),
92 "must have frame");
93 NS_ASSERTION(aContent || !(aHint & nsChangeHint_ReconstructFrame),
94 "must have content");
95 NS_ASSERTION(!aContent || aContent->IsNodeOfType(nsINode::eELEMENT),
96 "Shouldn't be trying to restyle non-elements directly");
98 if ((0 < mCount) && (aHint & nsChangeHint_ReconstructFrame)) { // filter out all other changes for same content
99 if (aContent) {
100 for (PRInt32 index = mCount - 1; index >= 0; --index) {
101 if (aContent == mArray[index].mContent) { // remove this change
102 aContent->Release();
103 mCount--;
104 if (index < mCount) { // move later changes down
105 ::memmove(&mArray[index], &mArray[index + 1],
106 (mCount - index) * sizeof(nsStyleChangeData));
113 PRInt32 last = mCount - 1;
114 if ((0 < mCount) && aFrame && (aFrame == mArray[last].mFrame)) { // same as last frame
115 NS_UpdateHint(mArray[last].mHint, aHint);
117 else {
118 if (mCount == mArraySize) {
119 PRInt32 newSize = mArraySize + kGrowArrayBy;
120 nsStyleChangeData* newArray = new nsStyleChangeData[newSize];
121 if (newArray) {
122 memcpy(newArray, mArray, mCount * sizeof(nsStyleChangeData));
123 if (mArray != mBuffer) {
124 delete [] mArray;
126 mArray = newArray;
127 mArraySize = newSize;
129 else {
130 return NS_ERROR_OUT_OF_MEMORY;
133 mArray[mCount].mFrame = aFrame;
134 mArray[mCount].mContent = aContent;
135 if (aContent) {
136 aContent->AddRef();
138 mArray[mCount].mHint = aHint;
139 mCount++;
141 return NS_OK;
144 void
145 nsStyleChangeList::Clear()
147 for (PRInt32 index = mCount - 1; index >= 0; --index) {
148 nsIContent* content = mArray[index].mContent;
149 if (content) {
150 content->Release();
153 if (mArray != mBuffer) {
154 delete [] mArray;
155 mArray = mBuffer;
156 mArraySize = kStyleChangeBufferSize;
158 mCount = 0;