Bug 54488 - "[Mac] Non-draggable widgets in background windows should look disabled...
[mozilla-central.git] / layout / base / nsImageLoader.cpp
blobf917dbb115c2127068d1feba7ac8b81e9336ba79
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
14 * License.
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.
23 * Contributor(s):
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"
46 #include "nsIURI.h"
47 #include "nsILoadGroup.h"
48 #include "nsNetUtil.h"
50 #include "nsPresContext.h"
51 #include "nsIPresShell.h"
52 #include "nsIFrame.h"
53 #include "nsIContent.h"
54 #include "nsIDocument.h"
56 #include "imgIContainer.h"
58 #include "nsStyleContext.h"
59 #include "nsGkAtoms.h"
61 // Paint forcing
62 #include "prenv.h"
64 NS_IMPL_ISUPPORTS2(nsImageLoader, imgIDecoderObserver, imgIContainerObserver)
66 nsImageLoader::nsImageLoader() :
67 mFrame(nsnull), mPresContext(nsnull)
71 nsImageLoader::~nsImageLoader()
73 mFrame = nsnull;
74 mPresContext = nsnull;
76 if (mRequest) {
77 mRequest->Cancel(NS_ERROR_FAILURE);
82 void
83 nsImageLoader::Init(nsIFrame *aFrame, nsPresContext *aPresContext,
84 PRBool aReflowOnLoad)
86 mFrame = aFrame;
87 mPresContext = aPresContext;
88 mReflowOnLoad = aReflowOnLoad;
91 void
92 nsImageLoader::Destroy()
94 mFrame = nsnull;
95 mPresContext = nsnull;
97 if (mRequest) {
98 mRequest->Cancel(NS_ERROR_FAILURE);
101 mRequest = nsnull;
104 nsresult
105 nsImageLoader::Load(imgIRequest *aImage)
107 if (!mFrame)
108 return NS_ERROR_NOT_INITIALIZED;
110 if (!aImage)
111 return NS_ERROR_FAILURE;
113 if (mRequest) {
114 nsCOMPtr<nsIURI> oldURI;
115 mRequest->GetURI(getter_AddRefs(oldURI));
116 nsCOMPtr<nsIURI> newURI;
117 aImage->GetURI(getter_AddRefs(newURI));
118 PRBool eq = PR_FALSE;
119 nsresult rv = newURI->Equals(oldURI, &eq);
120 if (NS_SUCCEEDED(rv) && eq) {
121 return NS_OK;
124 // Now cancel the old request so it won't hold a stale ref to us.
125 mRequest->Cancel(NS_ERROR_FAILURE);
126 mRequest = nsnull;
129 // Make sure to clone into a temporary, then set mRequest, since
130 // cloning may notify and we don't want to trigger paints from this
131 // code.
132 nsCOMPtr<imgIRequest> newRequest;
133 nsresult rv = aImage->Clone(this, getter_AddRefs(newRequest));
134 mRequest.swap(newRequest);
135 return rv;
140 NS_IMETHODIMP nsImageLoader::OnStartContainer(imgIRequest *aRequest,
141 imgIContainer *aImage)
143 if (aImage)
145 /* Get requested animation policy from the pres context:
146 * normal = 0
147 * one frame = 1
148 * one loop = 2
150 aImage->SetAnimationMode(mPresContext->ImageAnimationMode());
151 // Ensure the animation (if any) is started.
152 aImage->StartAnimation();
154 return NS_OK;
157 NS_IMETHODIMP nsImageLoader::OnStopFrame(imgIRequest *aRequest,
158 gfxIImageFrame *aFrame)
160 if (!mFrame)
161 return NS_ERROR_FAILURE;
163 #ifdef NS_DEBUG
164 // Make sure the image request status's STATUS_FRAME_COMPLETE flag has been set to ensure
165 // the image will be painted when invalidated
166 if (aRequest) {
167 PRUint32 status = imgIRequest::STATUS_ERROR;
168 nsresult rv = aRequest->GetImageStatus(&status);
169 if (NS_SUCCEEDED(rv)) {
170 NS_ASSERTION((status & imgIRequest::STATUS_FRAME_COMPLETE), "imgIRequest::STATUS_FRAME_COMPLETE not set");
173 #endif
175 if (!mRequest) {
176 // We're in the middle of a paint anyway
177 return NS_OK;
180 // Draw the background image
181 RedrawDirtyFrame(nsnull);
182 return NS_OK;
185 NS_IMETHODIMP nsImageLoader::FrameChanged(imgIContainer *aContainer,
186 gfxIImageFrame *newframe,
187 nsRect * dirtyRect)
189 if (!mFrame)
190 return NS_ERROR_FAILURE;
192 if (!mRequest) {
193 // We're in the middle of a paint anyway
194 return NS_OK;
197 nsRect r(*dirtyRect);
199 r.x = nsPresContext::CSSPixelsToAppUnits(r.x);
200 r.y = nsPresContext::CSSPixelsToAppUnits(r.y);
201 r.width = nsPresContext::CSSPixelsToAppUnits(r.width);
202 r.height = nsPresContext::CSSPixelsToAppUnits(r.height);
204 RedrawDirtyFrame(&r);
206 return NS_OK;
210 void
211 nsImageLoader::RedrawDirtyFrame(const nsRect* aDamageRect)
213 if (mReflowOnLoad) {
214 nsIPresShell *shell = mPresContext->GetPresShell();
215 #ifdef DEBUG
216 nsresult rv =
217 #endif
218 shell->FrameNeedsReflow(mFrame, nsIPresShell::eStyleChange, NS_FRAME_IS_DIRTY);
219 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Could not reflow after loading border-image");
220 // The reflow might not do all the invalidation we need, so continue
221 // on with the invalidation codepath.
223 // NOTE: It is not sufficient to invalidate only the size of the image:
224 // the image may be tiled!
225 // The best option is to call into the frame, however lacking this
226 // we have to at least invalidate the frame's bounds, hence
227 // as long as we have a frame we'll use its size.
230 // Invalidate the entire frame
231 // XXX We really only need to invalidate the client area of the frame...
233 nsRect bounds(nsPoint(0, 0), mFrame->GetSize());
235 if (mFrame->GetType() == nsGkAtoms::canvasFrame) {
236 // The canvas's background covers the whole viewport.
237 bounds = mFrame->GetOverflowRect();
240 // XXX this should be ok, but there is some crappy ass bug causing it not to work
241 // XXX seems related to the "body fixup rule" dealing with the canvas and body frames...
242 #if 0
243 // Invalidate the entire frame only if the frame has a tiled background
244 // image, otherwise just invalidate the intersection of the frame's bounds
245 // with the damaged rect.
246 nsStyleContext* styleContext;
247 mFrame->GetStyleContext(&styleContext);
248 const nsStyleBackground* bg = styleContext->GetStyleBackground();
250 if ((bg->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE) ||
251 (bg->mBackgroundRepeat == NS_STYLE_BG_REPEAT_OFF)) {
252 // The frame does not have a background image so we are free
253 // to invalidate only the intersection of the damage rect and
254 // the frame's bounds.
256 if (aDamageRect) {
257 bounds.IntersectRect(*aDamageRect, bounds);
261 #endif
263 mFrame->Invalidate(bounds);