2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 21:52:36 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-07-27 17:50:58 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* 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/
|
1998-07-27 17:50:58 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* 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.
|
1998-07-27 17:50:58 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-25 01:32:19 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:40:37 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Contributor(s):
|
2003-01-18 10:17:58 +00:00
|
|
|
* Morten Nilsen <morten@nilsen.com>
|
|
|
|
* Christian Biesinger <cbiesinger@web.de>
|
2003-10-21 15:35:57 +00:00
|
|
|
* Jan Varga <varga@nixcorp.com>
|
2004-04-17 21:52:36 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 21:52:36 +00:00
|
|
|
* 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"),
|
2001-09-25 01:32:19 +00:00
|
|
|
* 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
|
2004-04-17 21:52:36 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-25 01:32:19 +00:00
|
|
|
* 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
|
2004-04-17 21:52:36 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-25 01:32:19 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-03-22 01:42:32 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
#include "nsRect.h"
|
1998-09-09 16:19:30 +00:00
|
|
|
#include "nsHTMLDocument.h"
|
2003-01-18 10:17:58 +00:00
|
|
|
#include "nsIImageDocument.h"
|
2003-04-02 03:27:20 +00:00
|
|
|
#include "nsIImageLoadingContent.h"
|
2001-10-30 10:14:06 +00:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2003-01-18 10:17:58 +00:00
|
|
|
#include "nsIDOMHTMLImageElement.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMKeyEvent.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
1998-07-27 17:50:58 +00:00
|
|
|
#include "nsHTMLAtoms.h"
|
2001-03-22 01:42:32 +00:00
|
|
|
#include "imgIRequest.h"
|
|
|
|
#include "imgILoader.h"
|
2001-04-23 23:46:50 +00:00
|
|
|
#include "imgIContainer.h"
|
2003-01-18 10:17:58 +00:00
|
|
|
#include "imgIDecoderObserver.h"
|
1998-07-27 17:50:58 +00:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIPresContext.h"
|
2003-02-22 00:32:13 +00:00
|
|
|
#include "nsStyleContext.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2003-03-22 03:20:23 +00:00
|
|
|
#include "nsMediaDocument.h"
|
2004-01-28 00:18:22 +00:00
|
|
|
#include "nsStyleSet.h"
|
2001-02-06 23:39:46 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
#define AUTOMATIC_IMAGE_RESIZING_PREF "browser.enable_automatic_image_resizing"
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-02-18 00:18:44 +00:00
|
|
|
class nsImageDocument;
|
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
class ImageListener: public nsMediaDocumentStreamListener
|
2003-02-18 00:18:44 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ImageListener(nsImageDocument* aDocument);
|
|
|
|
virtual ~ImageListener();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
};
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
class nsImageDocument : public nsMediaDocument,
|
2003-01-18 10:17:58 +00:00
|
|
|
public nsIImageDocument,
|
|
|
|
public imgIDecoderObserver,
|
|
|
|
public nsIDOMEventListener
|
|
|
|
{
|
1998-07-27 17:50:58 +00:00
|
|
|
public:
|
|
|
|
nsImageDocument();
|
|
|
|
virtual ~nsImageDocument();
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
virtual nsresult Init();
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
virtual nsresult StartDocumentLoad(const char* aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
nsISupports* aContainer,
|
|
|
|
nsIStreamListener** aDocListener,
|
|
|
|
PRBool aReset = PR_TRUE,
|
|
|
|
nsIContentSink* aSink = nsnull);
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject);
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_DECL_NSIIMAGEDOCUMENT
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_DECL_IMGIDECODEROBSERVER
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_DECL_IMGICONTAINEROBSERVER
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
// nsIDOMEventListener
|
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent);
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-02-18 00:18:44 +00:00
|
|
|
friend class ImageListener;
|
2003-01-18 10:17:58 +00:00
|
|
|
protected:
|
|
|
|
nsresult CreateSyntheticDocument();
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
nsresult CheckOverflowing();
|
2001-02-21 20:38:08 +00:00
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
void UpdateTitleAndCharset();
|
2001-03-22 01:42:32 +00:00
|
|
|
|
2003-04-21 22:45:28 +00:00
|
|
|
float GetRatio() {
|
|
|
|
return PR_MIN((float)mVisibleWidth / mImageWidth,
|
|
|
|
(float)mVisibleHeight / mImageHeight);
|
|
|
|
}
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> mImageElement;
|
2001-03-22 01:42:32 +00:00
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
PRInt32 mVisibleWidth;
|
|
|
|
PRInt32 mVisibleHeight;
|
|
|
|
PRInt32 mImageWidth;
|
|
|
|
PRInt32 mImageHeight;
|
2001-03-22 01:42:32 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
PRPackedBool mImageResizingEnabled;
|
|
|
|
PRPackedBool mImageIsOverflowing;
|
|
|
|
PRPackedBool mImageIsResized;
|
|
|
|
};
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ImageListener, nsMediaDocumentStreamListener)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ImageListener, nsMediaDocumentStreamListener)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(ImageListener)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsMediaDocumentStreamListener)
|
2000-03-16 22:58:12 +00:00
|
|
|
|
2003-02-18 00:18:44 +00:00
|
|
|
ImageListener::ImageListener(nsImageDocument* aDocument)
|
2003-03-22 03:20:23 +00:00
|
|
|
: nsMediaDocumentStreamListener(aDocument)
|
2003-02-18 00:18:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
|
2003-02-18 00:18:44 +00:00
|
|
|
ImageListener::~ImageListener()
|
|
|
|
{
|
2003-09-07 21:50:21 +00:00
|
|
|
}
|
2003-02-18 00:18:44 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageListener::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
|
|
|
|
{
|
2003-04-04 00:26:33 +00:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
nsImageDocument *imgDoc = (nsImageDocument*)mDocument.get();
|
2003-02-18 00:18:44 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
if (!channel) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-04-02 03:27:20 +00:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(imgDoc->mImageElement);
|
|
|
|
NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
imageLoader->AddObserver(imgDoc);
|
|
|
|
imageLoader->LoadImageWithChannel(channel, getter_AddRefs(mNextStream));
|
2003-02-18 00:18:44 +00:00
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
return nsMediaDocumentStreamListener::OnStartRequest(request, ctxt);
|
2003-02-18 00:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImageListener::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
|
|
|
nsresult status)
|
|
|
|
{
|
2003-04-04 00:26:33 +00:00
|
|
|
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
|
2003-04-02 03:27:20 +00:00
|
|
|
nsImageDocument *imgDoc = (nsImageDocument*)mDocument.get();
|
2003-04-13 00:40:26 +00:00
|
|
|
imgDoc->UpdateTitleAndCharset();
|
2003-04-02 03:27:20 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(imgDoc->mImageElement);
|
2004-03-14 18:20:38 +00:00
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->RemoveObserver(imgDoc);
|
|
|
|
}
|
2003-02-18 00:18:44 +00:00
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
return nsMediaDocumentStreamListener::OnStopRequest(request, ctxt, status);
|
2003-02-18 00:18:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-26 07:41:30 +00:00
|
|
|
|
|
|
|
// NOTE! nsDocument::operator new() zeroes out all members, so don't
|
|
|
|
// bother initializing members to 0.
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
nsImageDocument::nsImageDocument()
|
|
|
|
{
|
2003-03-26 07:41:30 +00:00
|
|
|
|
|
|
|
// NOTE! nsDocument::operator new() zeroes out all members, so don't
|
|
|
|
// bother initializing members to 0.
|
|
|
|
|
1998-07-27 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
nsImageDocument::~nsImageDocument()
|
1998-07-27 17:50:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-03-26 07:41:30 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsImageDocument, nsMediaDocument)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsImageDocument, nsMediaDocument)
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsImageDocument)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIImageDocument)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(imgIDecoderObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(imgIContainerObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(ImageDocument)
|
2003-03-26 07:41:30 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsMediaDocument)
|
2002-01-25 06:37:35 +00:00
|
|
|
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
nsresult
|
|
|
|
nsImageDocument::Init()
|
|
|
|
{
|
2003-04-13 00:40:26 +00:00
|
|
|
nsresult rv = nsMediaDocument::Init();
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-01-25 06:37:35 +00:00
|
|
|
|
2004-04-29 23:34:19 +00:00
|
|
|
mImageResizingEnabled =
|
|
|
|
nsContentUtils::GetBoolPref(AUTOMATIC_IMAGE_RESIZING_PREF);
|
2002-01-25 06:37:35 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-07-27 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
nsresult
|
2003-01-18 10:17:58 +00:00
|
|
|
nsImageDocument::StartDocumentLoad(const char* aCommand,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsILoadGroup* aLoadGroup,
|
|
|
|
nsISupports* aContainer,
|
|
|
|
nsIStreamListener** aDocListener,
|
|
|
|
PRBool aReset,
|
|
|
|
nsIContentSink* aSink)
|
1998-07-27 17:50:58 +00:00
|
|
|
{
|
2003-03-26 07:41:30 +00:00
|
|
|
nsresult rv =
|
|
|
|
nsMediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup,
|
|
|
|
aContainer, aDocListener, aReset,
|
|
|
|
aSink);
|
1998-12-11 02:47:25 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2000-01-27 02:15:19 +00:00
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
NS_ASSERTION(aDocListener, "null aDocListener");
|
2003-04-04 00:26:33 +00:00
|
|
|
*aDocListener = new ImageListener(this);
|
|
|
|
if (!*aDocListener)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aDocListener);
|
1998-07-27 17:50:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
void
|
2002-12-13 23:53:43 +00:00
|
|
|
nsImageDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject)
|
|
|
|
{
|
|
|
|
if (!aScriptGlobalObject) {
|
2003-01-18 10:17:58 +00:00
|
|
|
if (mImageResizingEnabled) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mImageElement);
|
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("click"), this, PR_FALSE);
|
|
|
|
|
|
|
|
target = do_QueryInterface(mScriptGlobalObject);
|
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("resize"), this, PR_FALSE);
|
2003-03-26 07:41:30 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this,
|
|
|
|
PR_FALSE);
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
2003-04-02 03:27:20 +00:00
|
|
|
|
2004-03-11 20:47:51 +00:00
|
|
|
// Break reference cycle with mImageElement, if we have one
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageElement);
|
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
2003-04-02 03:27:20 +00:00
|
|
|
mImageElement = nsnull;
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
2003-03-20 02:03:13 +00:00
|
|
|
|
|
|
|
// Set the script global object on the superclass before doing
|
|
|
|
// anything that might require it....
|
2003-10-22 06:09:48 +00:00
|
|
|
nsHTMLDocument::SetScriptGlobalObject(aScriptGlobalObject);
|
2003-03-26 07:41:30 +00:00
|
|
|
|
2003-03-20 02:03:13 +00:00
|
|
|
if (aScriptGlobalObject) {
|
|
|
|
// Create synthetic document
|
2003-10-22 06:09:48 +00:00
|
|
|
nsresult rv = CreateSyntheticDocument();
|
2003-03-20 02:03:13 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2003-10-22 06:09:48 +00:00
|
|
|
return;
|
2003-03-20 02:03:13 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
if (mImageResizingEnabled) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mImageElement);
|
|
|
|
target->AddEventListener(NS_LITERAL_STRING("click"), this, PR_FALSE);
|
|
|
|
|
|
|
|
target = do_QueryInterface(aScriptGlobalObject);
|
|
|
|
target->AddEventListener(NS_LITERAL_STRING("resize"), this, PR_FALSE);
|
|
|
|
target->AddEventListener(NS_LITERAL_STRING("keypress"), this, PR_FALSE);
|
|
|
|
}
|
2002-12-13 23:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::GetImageResizingEnabled(PRBool* aImageResizingEnabled)
|
|
|
|
{
|
|
|
|
*aImageResizingEnabled = mImageResizingEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::GetImageIsOverflowing(PRBool* aImageIsOverflowing)
|
|
|
|
{
|
|
|
|
*aImageIsOverflowing = mImageIsOverflowing;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::GetImageIsResized(PRBool* aImageIsResized)
|
|
|
|
{
|
|
|
|
*aImageIsResized = mImageIsResized;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::ShrinkToFit()
|
|
|
|
{
|
|
|
|
if (mImageResizingEnabled) {
|
|
|
|
nsCOMPtr<nsIDOMHTMLImageElement> image = do_QueryInterface(mImageElement);
|
2003-04-21 22:45:28 +00:00
|
|
|
image->SetWidth(NSToCoordFloor(GetRatio() * mImageWidth));
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
mImageElement->SetAttribute(NS_LITERAL_STRING("style"),
|
2003-05-02 22:23:55 +00:00
|
|
|
NS_LITERAL_STRING("cursor: -moz-zoom-in"));
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
mImageIsResized = PR_TRUE;
|
2003-04-21 22:45:28 +00:00
|
|
|
|
|
|
|
UpdateTitleAndCharset();
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::RestoreImage()
|
|
|
|
{
|
|
|
|
if (mImageResizingEnabled) {
|
|
|
|
mImageElement->RemoveAttribute(NS_LITERAL_STRING("width"));
|
|
|
|
|
2003-05-02 22:23:55 +00:00
|
|
|
if (mImageIsOverflowing) {
|
|
|
|
mImageElement->SetAttribute(NS_LITERAL_STRING("style"),
|
|
|
|
NS_LITERAL_STRING("cursor: -moz-zoom-out"));
|
|
|
|
}
|
|
|
|
else {
|
2003-01-18 10:17:58 +00:00
|
|
|
mImageElement->RemoveAttribute(NS_LITERAL_STRING("style"));
|
|
|
|
}
|
|
|
|
|
|
|
|
mImageIsResized = PR_FALSE;
|
2003-04-21 22:45:28 +00:00
|
|
|
|
|
|
|
UpdateTitleAndCharset();
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::ToggleImageSize()
|
|
|
|
{
|
|
|
|
if (mImageResizingEnabled) {
|
|
|
|
if (mImageIsResized) {
|
|
|
|
RestoreImage();
|
|
|
|
}
|
|
|
|
else if (mImageIsOverflowing) {
|
|
|
|
ShrinkToFit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-02-26 05:51:32 +00:00
|
|
|
nsImageDocument::OnStartDecode(imgIRequest* aRequest)
|
2003-01-18 10:17:58 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-02-26 05:51:32 +00:00
|
|
|
nsImageDocument::OnStartContainer(imgIRequest* aRequest, imgIContainer* aImage)
|
2003-01-18 10:17:58 +00:00
|
|
|
{
|
|
|
|
aImage->GetWidth(&mImageWidth);
|
|
|
|
aImage->GetHeight(&mImageHeight);
|
|
|
|
if (mImageResizingEnabled) {
|
|
|
|
CheckOverflowing();
|
|
|
|
}
|
2004-02-08 20:58:38 +00:00
|
|
|
UpdateTitleAndCharset();
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-02-26 05:51:32 +00:00
|
|
|
nsImageDocument::OnStartFrame(imgIRequest* aRequest, gfxIImageFrame* aFrame)
|
2003-01-18 10:17:58 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::OnDataAvailable(imgIRequest* aRequest,
|
|
|
|
gfxIImageFrame* aFrame,
|
|
|
|
const nsRect* aRect)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::OnStopFrame(imgIRequest* aRequest,
|
|
|
|
gfxIImageFrame* aFrame)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::OnStopContainer(imgIRequest* aRequest,
|
|
|
|
imgIContainer* aImage)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::OnStopDecode(imgIRequest* aRequest,
|
|
|
|
nsresult status,
|
|
|
|
const PRUnichar* statusArg)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::FrameChanged(imgIContainer* aContainer,
|
|
|
|
gfxIImageFrame* aFrame,
|
|
|
|
nsRect* aDirtyRect)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageDocument::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsAutoString eventType;
|
|
|
|
aEvent->GetType(eventType);
|
2004-05-22 22:15:22 +00:00
|
|
|
if (eventType.EqualsLiteral("resize")) {
|
2003-01-18 10:17:58 +00:00
|
|
|
CheckOverflowing();
|
|
|
|
}
|
2004-05-22 22:15:22 +00:00
|
|
|
else if (eventType.EqualsLiteral("click")) {
|
2003-01-18 10:17:58 +00:00
|
|
|
ToggleImageSize();
|
|
|
|
}
|
2004-05-22 22:15:22 +00:00
|
|
|
else if (eventType.EqualsLiteral("keypress")) {
|
2003-01-18 10:17:58 +00:00
|
|
|
nsCOMPtr<nsIDOMKeyEvent> keyEvent = do_QueryInterface(aEvent);
|
|
|
|
PRUint32 charCode;
|
|
|
|
keyEvent->GetCharCode(&charCode);
|
|
|
|
// plus key
|
|
|
|
if (charCode == 0x2B) {
|
|
|
|
if (mImageIsResized) {
|
|
|
|
RestoreImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// minus key
|
|
|
|
else if (charCode == 0x2D) {
|
|
|
|
if (mImageIsOverflowing) {
|
|
|
|
ShrinkToFit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-07-27 17:50:58 +00:00
|
|
|
nsresult
|
|
|
|
nsImageDocument::CreateSyntheticDocument()
|
|
|
|
{
|
|
|
|
// Synthesize an html document that refers to the image
|
2003-03-22 03:20:23 +00:00
|
|
|
nsresult rv = nsMediaDocument::CreateSyntheticDocument();
|
2000-05-10 13:13:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
nsCOMPtr<nsIHTMLContent> body = do_QueryInterface(mBodyContent);
|
|
|
|
if (!body) {
|
|
|
|
NS_WARNING("no body on image document!");
|
|
|
|
return NS_ERROR_FAILURE;
|
1998-07-27 17:50:58 +00:00
|
|
|
}
|
|
|
|
|
2003-03-22 03:20:23 +00:00
|
|
|
nsCOMPtr<nsINodeInfo> nodeInfo;
|
2000-05-10 13:13:39 +00:00
|
|
|
rv = mNodeInfoManager->GetNodeInfo(nsHTMLAtoms::img, nsnull,
|
|
|
|
kNameSpaceID_None,
|
2003-06-13 20:10:01 +00:00
|
|
|
getter_AddRefs(nodeInfo));
|
2000-05-10 13:13:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
nsCOMPtr<nsIHTMLContent> image;
|
|
|
|
rv = NS_NewHTMLImageElement(getter_AddRefs(image), nodeInfo);
|
|
|
|
if (NS_FAILED(rv)) {
|
1998-07-27 17:50:58 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2000-05-19 04:48:43 +00:00
|
|
|
image->SetDocument(this, PR_FALSE, PR_TRUE);
|
2003-01-18 10:17:58 +00:00
|
|
|
mImageElement = do_QueryInterface(image);
|
2003-04-02 03:27:20 +00:00
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(image);
|
|
|
|
NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED);
|
1998-09-09 16:19:30 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCAutoString src;
|
2004-01-09 23:54:21 +00:00
|
|
|
mDocumentURI->GetSpec(src);
|
2000-04-16 11:19:26 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
NS_ConvertUTF8toUCS2 srcString(src);
|
2003-04-02 03:27:20 +00:00
|
|
|
// Make sure not to start the image load from here...
|
|
|
|
imageLoader->SetLoadingEnabled(PR_FALSE);
|
2003-03-19 03:47:09 +00:00
|
|
|
image->SetAttr(kNameSpaceID_None, nsHTMLAtoms::src, srcString, PR_FALSE);
|
2002-03-28 21:12:15 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
if (mStringBundle) {
|
|
|
|
const PRUnichar* formatString[1] = { srcString.get() };
|
2002-03-28 21:12:15 +00:00
|
|
|
nsXPIDLString errorMsg;
|
2003-03-26 07:41:30 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(str, "InvalidImage");
|
|
|
|
mStringBundle->FormatStringFromName(str.get(), formatString, 1,
|
|
|
|
getter_Copies(errorMsg));
|
2002-03-28 21:12:15 +00:00
|
|
|
|
2003-03-19 03:47:09 +00:00
|
|
|
image->SetAttr(kNameSpaceID_None, nsHTMLAtoms::alt, errorMsg, PR_FALSE);
|
2002-03-28 21:12:15 +00:00
|
|
|
}
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-01-18 10:17:58 +00:00
|
|
|
body->AppendChildTo(image, PR_FALSE, PR_FALSE);
|
2003-07-09 06:57:14 +00:00
|
|
|
imageLoader->SetLoadingEnabled(PR_TRUE);
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsImageDocument::CheckOverflowing()
|
|
|
|
{
|
2003-09-27 04:18:26 +00:00
|
|
|
nsIPresShell *shell = GetShellAt(0);
|
2003-01-18 10:17:58 +00:00
|
|
|
if (!shell) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresContext> context;
|
|
|
|
shell->GetPresContext(getter_AddRefs(context));
|
|
|
|
|
2004-02-01 10:09:07 +00:00
|
|
|
nsRect visibleArea = context->GetVisibleArea();
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mBodyContent);
|
2003-03-26 07:41:30 +00:00
|
|
|
nsRefPtr<nsStyleContext> styleContext =
|
2004-01-28 00:18:22 +00:00
|
|
|
context->StyleSet()->ResolveStyleFor(content, nsnull);
|
2003-01-18 10:17:58 +00:00
|
|
|
|
2003-05-15 03:42:21 +00:00
|
|
|
const nsStyleMargin* marginData = styleContext->GetStyleMargin();
|
2003-01-18 10:17:58 +00:00
|
|
|
nsMargin margin;
|
|
|
|
marginData->GetMargin(margin);
|
|
|
|
visibleArea.Deflate(margin);
|
|
|
|
|
|
|
|
nsStyleBorderPadding bPad;
|
|
|
|
styleContext->GetBorderPaddingFor(bPad);
|
|
|
|
bPad.GetBorderPadding(margin);
|
|
|
|
visibleArea.Deflate(margin);
|
|
|
|
|
|
|
|
float t2p;
|
2004-02-11 04:57:07 +00:00
|
|
|
t2p = context->TwipsToPixels();
|
2003-01-18 10:17:58 +00:00
|
|
|
mVisibleWidth = NSTwipsToIntPixels(visibleArea.width, t2p);
|
|
|
|
mVisibleHeight = NSTwipsToIntPixels(visibleArea.height, t2p);
|
1998-07-27 17:50:58 +00:00
|
|
|
|
2003-03-26 07:41:30 +00:00
|
|
|
mImageIsOverflowing =
|
|
|
|
mImageWidth > mVisibleWidth || mImageHeight > mVisibleHeight;
|
2003-01-18 10:17:58 +00:00
|
|
|
|
|
|
|
if (mImageIsOverflowing) {
|
|
|
|
ShrinkToFit();
|
|
|
|
}
|
|
|
|
else if (mImageIsResized) {
|
|
|
|
RestoreImage();
|
|
|
|
}
|
1998-07-27 17:50:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
void
|
|
|
|
nsImageDocument::UpdateTitleAndCharset()
|
2000-01-27 02:15:19 +00:00
|
|
|
{
|
2003-04-13 00:40:26 +00:00
|
|
|
nsCAutoString typeStr;
|
|
|
|
nsCOMPtr<imgIRequest> imageRequest;
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageElement);
|
|
|
|
if (imageLoader) {
|
|
|
|
imageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
|
|
|
|
getter_AddRefs(imageRequest));
|
|
|
|
}
|
2003-04-02 03:27:20 +00:00
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
if (imageRequest) {
|
|
|
|
nsXPIDLCString mimeType;
|
|
|
|
imageRequest->GetMimeType(getter_Copies(mimeType));
|
|
|
|
ToUpperCase(mimeType);
|
|
|
|
nsXPIDLCString::const_iterator start, end;
|
|
|
|
mimeType.BeginReading(start);
|
|
|
|
mimeType.EndReading(end);
|
|
|
|
nsXPIDLCString::const_iterator iter = end;
|
|
|
|
if (FindInReadable(NS_LITERAL_CSTRING("IMAGE/"), start, iter) &&
|
|
|
|
iter != end) {
|
|
|
|
// strip out "X-" if any
|
|
|
|
if (*iter == 'X') {
|
|
|
|
++iter;
|
|
|
|
if (iter != end && *iter == '-') {
|
2002-02-06 21:05:52 +00:00
|
|
|
++iter;
|
2003-04-13 00:40:26 +00:00
|
|
|
if (iter == end) {
|
|
|
|
// looks like "IMAGE/X-" is the type?? Bail out of here.
|
|
|
|
mimeType.BeginReading(iter);
|
2002-02-06 21:05:52 +00:00
|
|
|
}
|
2003-04-13 00:40:26 +00:00
|
|
|
} else {
|
|
|
|
--iter;
|
2002-02-06 21:05:52 +00:00
|
|
|
}
|
2002-01-15 23:34:32 +00:00
|
|
|
}
|
2003-04-13 00:40:26 +00:00
|
|
|
typeStr = Substring(iter, end);
|
2002-01-15 23:34:32 +00:00
|
|
|
} else {
|
2003-04-13 00:40:26 +00:00
|
|
|
typeStr = mimeType;
|
2001-02-06 23:39:46 +00:00
|
|
|
}
|
|
|
|
}
|
2003-04-21 22:45:28 +00:00
|
|
|
|
|
|
|
nsXPIDLString status;
|
|
|
|
if (mImageIsResized) {
|
|
|
|
nsAutoString ratioStr;
|
|
|
|
ratioStr.AppendInt(NSToCoordFloor(GetRatio() * 100));
|
|
|
|
|
|
|
|
const PRUnichar* formatString[1] = { ratioStr.get() };
|
|
|
|
mStringBundle->FormatStringFromName(NS_LITERAL_STRING("ScaledImage").get(),
|
|
|
|
formatString, 1,
|
|
|
|
getter_Copies(status));
|
|
|
|
}
|
|
|
|
|
2003-04-13 00:40:26 +00:00
|
|
|
static const char* const formatNames[4] =
|
|
|
|
{
|
|
|
|
"ImageTitleWithNeitherDimensionsNorFile",
|
|
|
|
"ImageTitleWithoutDimensions",
|
|
|
|
"ImageTitleWithDimesions",
|
|
|
|
"ImageTitleWithDimensionsAndFile",
|
|
|
|
};
|
|
|
|
|
2003-04-21 22:45:28 +00:00
|
|
|
nsMediaDocument::UpdateTitleAndCharset(typeStr, formatNames,
|
|
|
|
mImageWidth, mImageHeight, status);
|
2003-01-18 10:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewImageDocument(nsIDocument** aResult)
|
|
|
|
{
|
|
|
|
nsImageDocument* doc = new nsImageDocument();
|
|
|
|
if (!doc) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = doc->Init();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete doc;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult = doc);
|
|
|
|
|
1999-07-07 07:31:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|