1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
24 * Stuart Parmenter <pavlov@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* class to notify frames of background image loads */
42 #include "nsImageLoader.h"
44 #include "imgILoader.h"
47 #include "nsILoadGroup.h"
48 #include "nsNetUtil.h"
50 #include "nsPresContext.h"
51 #include "nsIPresShell.h"
53 #include "nsIContent.h"
54 #include "nsIDocument.h"
56 #include "imgIContainer.h"
58 #include "nsStyleContext.h"
59 #include "nsGkAtoms.h"
64 NS_IMPL_ISUPPORTS2(nsImageLoader
, imgIDecoderObserver
, imgIContainerObserver
)
66 nsImageLoader::nsImageLoader(nsIFrame
*aFrame
, PRUint32 aActions
,
67 nsImageLoader
*aNextLoader
)
70 mNextLoader(aNextLoader
)
74 nsImageLoader::~nsImageLoader()
79 mRequest
->CancelAndForgetObserver(NS_ERROR_FAILURE
);
83 /* static */ already_AddRefed
<nsImageLoader
>
84 nsImageLoader::Create(nsIFrame
*aFrame
, imgIRequest
*aRequest
,
85 PRUint32 aActions
, nsImageLoader
*aNextLoader
)
87 nsRefPtr
<nsImageLoader
> loader
=
88 new nsImageLoader(aFrame
, aActions
, aNextLoader
);
90 loader
->Load(aRequest
);
92 return loader
.forget();
96 nsImageLoader::Destroy()
98 // Destroy the chain with only one level of recursion.
99 nsRefPtr
<nsImageLoader
> list
= mNextLoader
;
100 mNextLoader
= nsnull
;
102 nsRefPtr
<nsImageLoader
> todestroy
= list
;
103 list
= todestroy
->mNextLoader
;
104 todestroy
->mNextLoader
= nsnull
;
105 todestroy
->Destroy();
111 mRequest
->CancelAndForgetObserver(NS_ERROR_FAILURE
);
118 nsImageLoader::Load(imgIRequest
*aImage
)
120 NS_ASSERTION(!mRequest
, "can't reuse image loaders");
123 return NS_ERROR_NOT_INITIALIZED
;
126 return NS_ERROR_FAILURE
;
128 // Make sure to clone into a temporary, then set mRequest, since
129 // cloning may notify and we don't want to trigger paints from this
131 nsCOMPtr
<imgIRequest
> newRequest
;
132 nsresult rv
= aImage
->Clone(this, getter_AddRefs(newRequest
));
133 mRequest
.swap(newRequest
);
139 NS_IMETHODIMP
nsImageLoader::OnStartContainer(imgIRequest
*aRequest
,
140 imgIContainer
*aImage
)
142 NS_ABORT_IF_FALSE(aImage
, "Who's calling us then?");
144 /* Get requested animation policy from the pres context:
149 aImage
->SetAnimationMode(mFrame
->PresContext()->ImageAnimationMode());
154 NS_IMETHODIMP
nsImageLoader::OnStopFrame(imgIRequest
*aRequest
,
158 return NS_ERROR_FAILURE
;
161 // We're in the middle of a paint anyway
165 // Take requested actions
166 if (mActions
& ACTION_REFLOW_ON_DECODE
) {
169 if (mActions
& ACTION_REDRAW_ON_DECODE
) {
175 NS_IMETHODIMP
nsImageLoader::OnStopRequest(imgIRequest
*aRequest
,
179 return NS_ERROR_FAILURE
;
182 // We're in the middle of a paint anyway
186 // Take requested actions
187 if (mActions
& ACTION_REFLOW_ON_LOAD
) {
190 if (mActions
& ACTION_REDRAW_ON_LOAD
) {
196 NS_IMETHODIMP
nsImageLoader::FrameChanged(imgIContainer
*aContainer
,
197 const nsIntRect
*aDirtyRect
)
200 return NS_ERROR_FAILURE
;
203 // We're in the middle of a paint anyway
207 nsRect r
= (*aDirtyRect
== nsIntRect::GetMaxSizedIntRect()) ?
208 nsRect(nsPoint(0, 0), mFrame
->GetSize()) :
209 aDirtyRect
->ToAppUnits(nsPresContext::AppUnitsPerCSSPixel());
217 nsImageLoader::DoReflow()
219 nsIPresShell
*shell
= mFrame
->PresContext()->GetPresShell();
220 shell
->FrameNeedsReflow(mFrame
, nsIPresShell::eStyleChange
, NS_FRAME_IS_DIRTY
);
224 nsImageLoader::DoRedraw(const nsRect
* aDamageRect
)
226 // NOTE: It is not sufficient to invalidate only the size of the image:
227 // the image may be tiled!
228 // The best option is to call into the frame, however lacking this
229 // we have to at least invalidate the frame's bounds, hence
230 // as long as we have a frame we'll use its size.
233 // Invalidate the entire frame
234 // XXX We really only need to invalidate the client area of the frame...
236 nsRect
bounds(nsPoint(0, 0), mFrame
->GetSize());
238 if (mFrame
->GetType() == nsGkAtoms::canvasFrame
) {
239 // The canvas's background covers the whole viewport.
240 bounds
= mFrame
->GetVisualOverflowRect();
243 // XXX this should be ok, but there is some crappy ass bug causing it not to work
244 // XXX seems related to the "body fixup rule" dealing with the canvas and body frames...
246 // Invalidate the entire frame only if the frame has a tiled background
247 // image, otherwise just invalidate the intersection of the frame's bounds
248 // with the damaged rect.
249 nsStyleContext
* styleContext
;
250 mFrame
->GetStyleContext(&styleContext
);
251 const nsStyleBackground
* bg
= styleContext
->GetStyleBackground();
253 if ((bg
->mBackgroundFlags
& NS_STYLE_BG_IMAGE_NONE
) ||
254 (bg
->mBackgroundRepeat
== NS_STYLE_BG_REPEAT_OFF
)) {
255 // The frame does not have a background image so we are free
256 // to invalidate only the intersection of the damage rect and
257 // the frame's bounds.
260 bounds
.IntersectRect(*aDamageRect
, bounds
);
266 if (mFrame
->GetStyleVisibility()->IsVisible()) {
267 mFrame
->Invalidate(bounds
);