2001-08-10 00:22:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2004-04-18 18:30:37 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
2001-08-10 00:22:39 +04:00
|
|
|
* The Original Code is mozilla.org code.
|
2004-04-18 18:30:37 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
2001-08-10 00:22:39 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <pavlov@netscape.com>
|
2004-04-18 18:30:37 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-08-10 00:22:39 +04:00
|
|
|
|
2006-03-30 09:56:38 +04:00
|
|
|
/* class to notify frames of background image loads */
|
|
|
|
|
2001-08-10 00:22:39 +04:00
|
|
|
#include "nsImageLoader.h"
|
|
|
|
|
|
|
|
#include "imgILoader.h"
|
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
2001-08-10 00:22:39 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
|
2002-03-15 11:29:08 +03:00
|
|
|
#include "imgIContainer.h"
|
|
|
|
|
2003-02-22 03:32:13 +03:00
|
|
|
#include "nsStyleContext.h"
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2001-08-10 00:22:39 +04:00
|
|
|
|
2004-02-20 09:00:35 +03:00
|
|
|
// Paint forcing
|
|
|
|
#include "prenv.h"
|
|
|
|
|
2001-08-10 00:22:39 +04:00
|
|
|
NS_IMPL_ISUPPORTS2(nsImageLoader, imgIDecoderObserver, imgIContainerObserver)
|
|
|
|
|
|
|
|
nsImageLoader::nsImageLoader() :
|
|
|
|
mFrame(nsnull), mPresContext(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsImageLoader::~nsImageLoader()
|
|
|
|
{
|
|
|
|
mFrame = nsnull;
|
|
|
|
mPresContext = nsnull;
|
|
|
|
|
|
|
|
if (mRequest) {
|
|
|
|
mRequest->Cancel(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2004-08-01 03:15:21 +04:00
|
|
|
nsImageLoader::Init(nsIFrame *aFrame, nsPresContext *aPresContext)
|
2001-08-10 00:22:39 +04:00
|
|
|
{
|
|
|
|
mFrame = aFrame;
|
|
|
|
mPresContext = aPresContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsImageLoader::Destroy()
|
|
|
|
{
|
|
|
|
mFrame = nsnull;
|
|
|
|
mPresContext = nsnull;
|
|
|
|
|
|
|
|
if (mRequest) {
|
|
|
|
mRequest->Cancel(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
mRequest = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2004-03-09 06:57:51 +03:00
|
|
|
nsImageLoader::Load(imgIRequest *aImage)
|
2001-08-10 00:22:39 +04:00
|
|
|
{
|
|
|
|
if (!mFrame)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2004-03-09 06:57:51 +03:00
|
|
|
if (!aImage)
|
2002-02-15 02:31:54 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2001-08-10 00:22:39 +04:00
|
|
|
if (mRequest) {
|
|
|
|
nsCOMPtr<nsIURI> oldURI;
|
|
|
|
mRequest->GetURI(getter_AddRefs(oldURI));
|
2004-03-09 06:57:51 +03:00
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
aImage->GetURI(getter_AddRefs(newURI));
|
2001-08-10 00:22:39 +04:00
|
|
|
PRBool eq = PR_FALSE;
|
2004-03-09 06:57:51 +03:00
|
|
|
nsresult rv = newURI->Equals(oldURI, &eq);
|
|
|
|
if (NS_SUCCEEDED(rv) && eq) {
|
2001-08-10 00:22:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-03-17 19:09:02 +03:00
|
|
|
|
|
|
|
// Now cancel the old request so it won't hold a stale ref to us.
|
|
|
|
mRequest->Cancel(NS_ERROR_FAILURE);
|
2004-03-09 06:57:51 +03:00
|
|
|
mRequest = nsnull;
|
2001-08-10 00:22:39 +04:00
|
|
|
}
|
|
|
|
|
2006-01-27 04:11:50 +03:00
|
|
|
// Make sure to clone into a temporary, then set mRequest, since
|
|
|
|
// cloning may notify and we don't want to trigger paints from this
|
|
|
|
// code.
|
|
|
|
nsCOMPtr<imgIRequest> newRequest;
|
|
|
|
nsresult rv = aImage->Clone(this, getter_AddRefs(newRequest));
|
|
|
|
mRequest.swap(newRequest);
|
|
|
|
return rv;
|
2001-08-10 00:22:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-26 08:51:32 +03:00
|
|
|
NS_IMETHODIMP nsImageLoader::OnStartContainer(imgIRequest *aRequest,
|
|
|
|
imgIContainer *aImage)
|
2001-08-10 00:22:39 +04:00
|
|
|
{
|
2002-03-15 11:29:08 +03:00
|
|
|
if (aImage)
|
|
|
|
{
|
|
|
|
/* Get requested animation policy from the pres context:
|
|
|
|
* normal = 0
|
|
|
|
* one frame = 1
|
|
|
|
* one loop = 2
|
|
|
|
*/
|
2003-12-26 07:24:11 +03:00
|
|
|
aImage->SetAnimationMode(mPresContext->ImageAnimationMode());
|
2004-01-08 21:29:28 +03:00
|
|
|
// Ensure the animation (if any) is started.
|
|
|
|
aImage->StartAnimation();
|
2002-03-15 11:29:08 +03:00
|
|
|
}
|
2001-08-10 00:22:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-02-26 08:51:32 +03:00
|
|
|
NS_IMETHODIMP nsImageLoader::OnStopFrame(imgIRequest *aRequest,
|
|
|
|
gfxIImageFrame *aFrame)
|
2001-08-10 00:22:39 +04:00
|
|
|
{
|
2002-03-09 03:15:46 +03:00
|
|
|
if (!mFrame)
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-03-23 07:33:37 +03:00
|
|
|
|
|
|
|
#ifdef NS_DEBUG
|
|
|
|
// Make sure the image request status's STATUS_FRAME_COMPLETE flag has been set to ensure
|
|
|
|
// the image will be painted when invalidated
|
|
|
|
if (aRequest) {
|
|
|
|
PRUint32 status = imgIRequest::STATUS_ERROR;
|
|
|
|
nsresult rv = aRequest->GetImageStatus(&status);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION((status & imgIRequest::STATUS_FRAME_COMPLETE), "imgIRequest::STATUS_FRAME_COMPLETE not set");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-03-09 03:15:46 +03:00
|
|
|
|
2004-03-09 06:57:51 +03:00
|
|
|
if (!mRequest) {
|
|
|
|
// We're in the middle of a paint anyway
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-03-09 03:15:46 +03:00
|
|
|
// Draw the background image
|
|
|
|
RedrawDirtyFrame(nsnull);
|
2001-08-10 00:22:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-02-26 08:51:32 +03:00
|
|
|
NS_IMETHODIMP nsImageLoader::FrameChanged(imgIContainer *aContainer,
|
|
|
|
gfxIImageFrame *newframe,
|
|
|
|
nsRect * dirtyRect)
|
2001-08-10 00:22:39 +04:00
|
|
|
{
|
|
|
|
if (!mFrame)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2004-03-09 06:57:51 +03:00
|
|
|
if (!mRequest) {
|
|
|
|
// We're in the middle of a paint anyway
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-08-10 00:22:39 +04:00
|
|
|
nsRect r(*dirtyRect);
|
|
|
|
|
2007-02-07 10:46:44 +03:00
|
|
|
r.x = nsPresContext::CSSPixelsToAppUnits(r.x);
|
|
|
|
r.y = nsPresContext::CSSPixelsToAppUnits(r.y);
|
|
|
|
r.width = nsPresContext::CSSPixelsToAppUnits(r.width);
|
|
|
|
r.height = nsPresContext::CSSPixelsToAppUnits(r.height);
|
2001-08-10 00:22:39 +04:00
|
|
|
|
|
|
|
RedrawDirtyFrame(&r);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsImageLoader::RedrawDirtyFrame(const nsRect* aDamageRect)
|
|
|
|
{
|
|
|
|
// NOTE: It is not sufficient to invalidate only the size of the image:
|
|
|
|
// the image may be tiled!
|
|
|
|
// The best option is to call into the frame, however lacking this
|
|
|
|
// we have to at least invalidate the frame's bounds, hence
|
|
|
|
// as long as we have a frame we'll use its size.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Invalidate the entire frame
|
|
|
|
// XXX We really only need to invalidate the client area of the frame...
|
2004-03-10 06:09:05 +03:00
|
|
|
|
|
|
|
nsRect bounds(nsPoint(0, 0), mFrame->GetSize());
|
2001-08-10 00:22:39 +04:00
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
if (mFrame->GetType() == nsGkAtoms::canvasFrame) {
|
2006-03-27 02:12:54 +04:00
|
|
|
// The canvas's background covers the whole viewport.
|
|
|
|
bounds = mFrame->GetOverflowRect();
|
|
|
|
}
|
|
|
|
|
2001-08-10 00:22:39 +04:00
|
|
|
// XXX this should be ok, but there is some crappy ass bug causing it not to work
|
|
|
|
// XXX seems related to the "body fixup rule" dealing with the canvas and body frames...
|
|
|
|
#if 0
|
|
|
|
// Invalidate the entire frame only if the frame has a tiled background
|
|
|
|
// image, otherwise just invalidate the intersection of the frame's bounds
|
|
|
|
// with the damaged rect.
|
2003-02-22 03:32:13 +03:00
|
|
|
nsStyleContext* styleContext;
|
|
|
|
mFrame->GetStyleContext(&styleContext);
|
2003-05-15 07:42:21 +04:00
|
|
|
const nsStyleBackground* bg = styleContext->GetStyleBackground();
|
2001-08-10 00:22:39 +04:00
|
|
|
|
|
|
|
if ((bg->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE) ||
|
|
|
|
(bg->mBackgroundRepeat == NS_STYLE_BG_REPEAT_OFF)) {
|
|
|
|
// The frame does not have a background image so we are free
|
|
|
|
// to invalidate only the intersection of the damage rect and
|
|
|
|
// the frame's bounds.
|
|
|
|
|
|
|
|
if (aDamageRect) {
|
|
|
|
bounds.IntersectRect(*aDamageRect, bounds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-03-10 06:09:05 +03:00
|
|
|
mFrame->Invalidate(bounds, PR_FALSE);
|
2001-08-10 00:22:39 +04:00
|
|
|
}
|